1、POM:
- <!-- spring-redis -->
- <!-- config junit jar -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- <!-- config redis data and client jar -->
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- </dependency>
- <!-- config need jar -->
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- <dependency>
- <groupId>org.apache.geronimo.specs</groupId>
- <artifactId>geronimo-servlet_3.0_spec</artifactId>
- <version>1.0</version>
- </dependency>
- <!-- cofig spring jar -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${org.springframework.version}</version>
- </dependency>
- # Redis 参数配置
- redis.isopen:yes
- redis.host:172.16.30.23
- redis.port:6379
- redis.pass:
- redis.maxIdle:300
- redis.maxActive:600
- redis.maxWait:1000
- redis.testOnBorrow:true
- <!-- 注意此处注入的是JedisPoolConfig,说明SDR还依赖与Jedis -->
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxIdle" value="${redis.maxIdle}" />
- <property name="maxActive" value="${redis.maxActive}" />
- <property name="maxWait" value="${redis.maxWait}" />
- <property name="testOnBorrow" value="${redis.testOnBorrow}" />
- </bean>
- <bean id="connectionFactory"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"
- p:pool-config-ref="poolConfig" />
- <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
- <property name="connectionFactory" ref="connectionFactory" />
- <!-- 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast
- to String!!! -->
- <property name="keySerializer">
- <bean
- class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- </property>
- <property name="valueSerializer">
- <bean
- class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
- </property>
- </bean>
- public void add(final User user){
- //方式1:.在redistemplate中配置Serializer
- redisTemplate.execute(new RedisCallback<Object>() {
- @Override
- public Object doInRedis(RedisConnection connection)
- throws DataAccessException {
- connection.set(redisTemplate.getStringSerializer().serialize(user.getId()), redisTemplate.getStringSerializer().serialize(user.getName()));
- return null;
- }
- });
- //方式2:不在redistemplate中配置Serializer,而是在Service的实现类中单独指定Serializer。
- ValueOperations<Serializable, Serializable> valueOps = redisTemplate.opsForValue();
- valueOps.set(user.getId(), user);
- }
- public User get(final String userId) {
- //方式1:.在redistemplate中配置Serializer
- return redisTemplate.execute(new RedisCallback<User>() {
- @Override
- public User doInRedis(RedisConnection connection)
- throws DataAccessException {
- byte[] key = redisTemplate.getStringSerializer().serialize(userId);
- User user = new User();
- if(connection.exists(key)){
- byte[] value = connection.get(key);
- String name = redisTemplate.getStringSerializer().deserialize(value);
- user.setId(userId);
- user.setName(name);
- }
- return user;
- }
- });
- //方式2:不在redistemplate中配置Serializer,而是在Service的实现类中单独指定Serializer。
- BoundValueOperations<Serializable, Serializable> boundValueOps = redisTemplate.boundValueOps(userId);
- User serializable = (User) boundValueOps.get();
- System.out.println(serializable);
- return serializable;