springdata+redis配置详解
springdata设计初衷是位简化数据类型和数据的持久化存储,它并不局限是关系型数据库还是nosql数据库,都提供了简化的数据库连接,让数据获取变得更加的简单。所有这些的实现有统一的api提供。
本文主要设置spring-data-redis的相关配置特性:
1.RedisTemplate:高度封装的,自动连接池管理类;
2.对数据类型进行了归类,封装了操作接口类:
a) ValueOperations:key-value操作
b) setOperations:set的相关操作
c) ZsetOperations:
d) HashOperations:hash数据类型操作
e) ListOperations:list数据类型操作
3.对事务进行 封装,通过容器进行控制。
4.序列化机制,提供各种序列化策略选择。
集成配置详解:
1.提供简单封装保存查询操作接口,以及实现类。
1 public interface RedisCommand { 2 3 public void save(String key,String value); 4 5 public String get(String key); 6 7 public <T> T getObject(String key,Class<T> clazz); 8 9 }
1 @Service 2 public class RedisHandle implements RedisCommand { 3 4 @Autowired 5 protected RedisTemplate<String, String> redisTemplate; 6 7 @Override 8 public void save(final String key, final String value) { 9 redisTemplate.execute(new RedisCallback<Object>() { 10 @Override 11 public Object doInRedis(RedisConnection connection) 12 throws DataAccessException { 13 connection.set( 14 redisTemplate.getStringSerializer().serialize( 15 "user.uid." + key), redisTemplate 16 .getStringSerializer().serialize(value)); 17 return null; 18 } 19 }); 20 } 21 22 @Override 23 public String get(final String key) { 24 return redisTemplate.execute(new RedisCallback<String>() { 25 @Override 26 public String doInRedis(RedisConnection connection) 27 throws DataAccessException { 28 byte[] keys = redisTemplate.getStringSerializer().serialize( 29 "user.uid." + key); 30 if (connection.exists(keys)) { 31 byte[] value = connection.get(keys); 32 String name = redisTemplate.getStringSerializer() 33 .deserialize(value); 34 return name; 35 } 36 return null; 37 } 38 }); 39 } 40 41 @Override 42 public <T> T getObject(String key, Class<T> clazz) { 43 // TODO Auto-generated method stub 44 return null; 45 } 46 47 48 }
2.业务逻辑类
1 @Service(value="userRedis") 2 public class UserRedisImpl implements UserRedis{ 3 4 @Autowired 5 protected RedisHandle handler; 6 7 @Override 8 public void saveUser(final User user) { 9 handler.save(user.getId()+"", JSONObject.toJSONString(user)); 10 } 11 12 @Override 13 public User getUser(final long id) { 14 String value =handler.get(id+""); 15 User u= JSONObject.parseObject(value, User.class); 16 return u; 17 } 18 }
3.测试类:
1 public class TestUseRedis { 2 3 @SuppressWarnings("resource") 4 public static void main(String[] args) { 5 ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/spring-redis.xml"); 6 UserRedis userDAO = (UserRedis)ac.getBean("userRedis"); 7 System.out.println("userDao"+JSONObject.toJSONString(userDAO)); 8 User user1 = new User(); 9 user1.setId(4); 10 user1.setName("oba2sssss220a"); 11 userDAO.saveUser(user1); 12 User user2 = userDAO.getUser(2); 13 System.out.println(user2.getName()); 14 } 15 }
4.配置文件相关:
spring-redis:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 10 11 <context:property-placeholder location="classpath:redis.properties" /> 12 <context:component-scan base-package="com.hoo.report.web.service"> 13 </context:component-scan> 14 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 15 <property name="maxIdle" value="${redis.maxIdle}" /> 16 <property name="maxActive" value="${redis.maxActive}" /> 17 <property name="maxWait" value="${redis.maxWait}" /> 18 <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 19 </bean> 20 21 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 22 p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> 23 24 <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 25 <property name="connectionFactory" ref="connectionFactory" /> 26 </bean> 27 28 </beans>
pom引用:
1 <!-- spring data --> 2 <dependency> 3 <groupId>org.springframework.data</groupId> 4 <artifactId>spring-data-redis</artifactId> 5 <version>1.0.2.RELEASE</version> 6 </dependency> 7 8 <dependency> 9 <groupId>redis.clients</groupId> 10 <artifactId>jedis</artifactId> 11 <version>2.1.0</version> 12 </dependency>
redis.properties:
1 redis.host=127.0.0.1 2 redis.port=6379 3 redis.pass= 4 5 redis.maxIdle=300 6 redis.maxActive=600 7 redis.maxWait=1000 8 redis.testOnBorrow=true
do good things。