spring data redis 指南

前言

Spring-DATA-Redis 项目采用Spring核心概念,使用一个key-value式的数据存储解决方案的开发。我们提供了一个“template”作为用于发送和接收消息的高级抽象。你会发现相似的JDBC支持的Spring框架

配置Jedis连接

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   <!-- Jedis ConnectionFactory --> 
   <bean id="jedisConnectionFactory"
         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/>
 </beans>

  当然在实际生产环境中我们还需要配置一下主机,端口 ,用户名,密码之类

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="jedisConnectionFactory" 
         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
             p:host-name="server" p:port="6379" />
</beans>

配置JRedis连接

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <bean id="jredisConnectionFactory" 
        class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory"
         p:host-name="server" p:port="6379"/> 
</beans>

   配置JRedis的连接池

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="jredisConnectionFactory" 
     class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory">
      <constructor-arg> 
          <bean class="org.springframework.data.redis.connection.jredis.DefaultJredisPool">
               <constructor-arg value="localhost" />
               <constructor-arg value="6379" /> 
          </bean> 
      </constructor-arg>
   </bean> 
</beans>

Configuring SRP connector 

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="srpConnectionFactory" 
     class="org.springframework.data.redis.connection.srp.SrpConnectionFactory" 
     p:host-name="server" p:port="6379"/> 
 </beans>

Configuring Lettuce connector

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
        <bean id="lettuceConnectionFactory" 
            class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory" 
            p:host-name="server" p:port="6379"/>
 </beans>

Redis Sentinel 支持

@Bean public RedisConnectionFactory jedisConnectionFactory() { 
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster") 
    .sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380); 
    return new JedisConnectionFactory(sentinelConfig); 
}


了解和使用RedisTemplate

方法参考

ValueOperations : Redis String(or value) operations

ListOperations : Redis list operations

SetOperations : Redis set operations

ZSetOperations :Redis zset(or sorted set) operations

HashOperations : redis hash operations


BoundValueOperations : Redis String(or value) key bound operations

BoundListOperations : Redis list key bound operations

BoundSetOperations : Redis set key bound operations

BoundZSetOpertions : Redis zset key bound operations

BoundHashOpertions : Redis hash key bound opertions 


如何使用RedisTemplate(jedis 使用了pool)

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectio            nFactory" p:use-pool="true"/>
  <!-- redis template definition -->
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-facto        ry-ref="jedisConnectionFactory"/> 
   
</beans>
public class Example { 
     // inject the actual template 
     @Autowired 
     private RedisTemplate<String, String> template;
     // inject the template as ListOperations 
     @Resource(name="redisTemplate") 
     private ListOperations<String, String> listOps;
     
     public void addLink(String userId, URL url) {   
        listOps.leftPush(userId, url.toExternalForm()); 
     }
 }


StringRedisTemplate

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectio        nFactory" p:use-pool="true"/> 
 <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connec        tion-factory-ref="jedisConnectionFactory"/> 
  </beans>
public class Example { 
@Autowired private StringRedisTemplate redisTemplate;
 public void addLink(String userId, URL url) {  
    redisTemplate.opsForList().leftPush(userId, url.toExternalForm());
   }
 }

Redis Transactions

//execute a transaction List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() { 
public List<Object> execute(RedisOperations operations) throws            DataAccessException{  
   operations.multi();
   operations.opsForSet().add("key", "value1"); 
   // This will contain the results of all ops in the transaction
    return operations.exec();
     }
}); 
 System.out.println("Number of items added to set: " + txResults.get(0));


你可能感兴趣的:(spring data redis 指南)