Step 1: Introduce the following jar in the porm.xml file
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> < version
>2.7.3</version>
</dependency>
Step 2: Write the configuration file (local-redis.properties)
#master IP
redis.master.host=192.168.4.92
#master Port
redis.master.port=6379
#slave IP
redis.slaver.host=192.168.4.92
#slave Port
redis.slaver.port=6379 #Maximum
number of objects allocated
redis.pool.maxTotal=1024 #Maximum number of objects that
can maintain the idle state
redis.pool.maxIdle=200 #When
no objects are returned in the pool, the maximum waiting time
redis.pool .maxWaitMillis=60000 #When
calling the borrow Object method, whether to check the validity
redis.pool.testOnBorrow=true #How
often to check the idle connection in the connection pool
redis.pool.timeBetweenEvictionRunsMillis=30000 #How
long will the idle connection be recovered
redis.pool.minEvictableIdleTimeMillis=30000
redis.pool.softMinEvictableIdleTimeMillis=10000
redis.pool.numTestsPerEvictionRun=1024 #Cache
expiration time in seconds 1000*60*60*24*7 seven days
#redis.pool.expire=604800000 #Whether
to open the Redis service application
redis.pool.unlock=false
redis.pool.testWhileIdle=true
redis.pool.testOnReturn=true
redis.pool.jmxEnabled=true
redis.pool.blockWhenExhausted=false
Step 3: Write the configuration file (redis.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 加载公用的jdbc和db配置文件 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
</bean> -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
<property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}" />
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
<property name="jmxEnabled" value="${redis.pool.jmxEnabled}" />
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}" />
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean name="slaver" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.slaver.host}" />
<constructor-arg index="1" value="${redis.slaver.port}"
type="int" />
</bean>
<bean name="master" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.master.host}" />
<constructor-arg index="1" value="${redis.master.port}"
type="int" />
</bean>
</list>
</constructor-arg> //Injection Step 4: Dependency injection (inject redis object in spring.xml) </beans>
</bean>
<import resource="redis.xml"/>
//Scan
<context:component-scan base-package="com.hiersun.ecommerce.redis">
<context:exclude-filter type="annotation" expression="org. springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
Step 5: Write redis The servicejava file (RedisBaseService.java, implementation class RedisBaseServiceImpl.java)
test example: (insert the session object into redis (that is, store it in memory))
//SET Session
HttpSession session = request.getSession();
session.setAttribute( WebCnts.SESSION_KEY_LOGINUSER, user);
redisBaseService.setObj(String.valueOf(user.getId()), user);
SessionUser sUser = (SessionUser)redisBaseService.getObj(String.valueOf(user.getId()));
logger.debug("Login>>>>>>>sUserName = "+sUser.getUname());
spring mvc+mybaties+maven+redis
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=327106412&siteId=291194637
Recommended
Ranking