Spring Redis(2)数据访问

Redis数据访问

RedisTemplate

Redis主要操作接口
接口作用
基本类型操作-
ValueOperationsRedis string操作
ListOperationsRedis list 操作
SetOperationsRedis set 操作
ZSetOperationsRedis zset 操作
HashOperationsRedis hash 操作
HyperLogLogOperationsRedis HyperLogLog operations like (pfadd, pfcount,…​)
GeoOperationsRedis geospatial operations like GEOADD, GEORADIUS,…​)
批量操作-
BoundValueOperationsRedis string (or value) key批量操
BoundListOperationsRedis list key 批量操
BoundSetOperationsRedis set key 批量操
BoundZSetOperationsRedis zset (or sorted set) key 批量操
BoundHashOperationsRedis hash key 批量操
BoundGeoOperationsRedis key bound geospatial operations.
定义RedisTemplate


<?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:use-pool="true"/>
  <!-- redis template definition -->
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
  ...

</beans>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/spring/spring-context.xml" })
public class RedisTemplateTest {

    // inject the actual template
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate<String, String> redisTemplate;

    // inject the template as ListOperations
    @Resource(name = "redisTemplate")
    private ListOperations<String, String> listOps;

    /**
     * 
     * Description: 展示String数据结构操作<br>
     * Create By: <br>
     * Create Date: 2017年3月20日 上午9:06:12
     *
     */
    @Test
    public void testValueOperations() {
        String key = "RedisTemplateTest:TEST:String:1";
        ValueOperations<String, String> valueOp = redisTemplate.opsForValue();
        valueOp.set(key, "1");
        Assert.assertEquals("1", valueOp.get(key));
        redisTemplate.delete(key);
        Assert.assertNull(valueOp.get(key));

    }

    /**
     * 
     * Description: 展示Set数据结构操作<br>
     * Create By: <br>
     * Create Date: 2017年3月20日 上午9:06:12
     *
     */
    @Test
    public void testSetOperations() {
        String key = "RedisTemplateTest:TEST:Set:1";
        SetOperations<String, String> op = redisTemplate.opsForSet();
        op.add(key, "1", "2");
        op.add(key, "2", "3", "4");
        op.remove(key, "4");
        Assert.assertEquals(Long.valueOf(3), op.size(key));
        redisTemplate.delete(key);
        Assert.assertEquals(Long.valueOf(0), op.size(key));

    }

    /**
     * 
     * Description: 展示SortedSet数据结构操作<br>
     * Create By: <br>
     * Create Date: 2017年3月20日 上午9:06:12
     *
     */
    @Test
    public void testZSetOperations() {
        String key = "RedisTemplateTest:TEST:ZSet:1";
        ZSetOperations<String, String> op = redisTemplate.opsForZSet();
        // 添加Key及用于排序的得分
        op.add(key, "1", 1);
        op.add(key, "2", 2);
        op.add(key, "3", 3);
        op.add(key, "4", 2);
        // 更新
        op.add(key, "4", 4);

        Assert.assertEquals(Long.valueOf(2), op.count(key, 3, 4));

        op.remove(key, "3");
        Assert.assertEquals(Long.valueOf(3), op.size(key));

        redisTemplate.delete(key);
        Assert.assertEquals(Long.valueOf(0), op.size(key));

    }

    /**
     * Description: 展示Hash数据结构操作<br>
     * Create By: <br>
     * Create Date: 2017年3月20日 上午9:06:12
     */
    @Test
    public void testHashOperations() {
        String key = "RedisTemplateTest:TEST:Hash:1";
        HashOperations<String, String, String> op = redisTemplate.opsForHash();

        op.put(key, "1", "1");

        Map<String, String> map = new HashMap<String, String>();
        map.put("2", "2");
        map.put("3", "3");
        map.put("4", "4");
        map.put("5", "5");

        op.putAll(key, map);

        Assert.assertEquals(5, op.entries(key).size());

        op.delete(key, "4", "5");
        Assert.assertEquals(3, op.entries(key).size());

        redisTemplate.delete(key);
        Assert.assertEquals(0, op.entries(key).size());

    }
}

StringRedisTemplate

org.springframework.data.redis.core.StringRedisTemplate类提供了基于String类型操作类,可用于简化Key、Value都是String类型时的操作。



<?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:use-pool="true"/>

  <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
  ...
</beans>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/spring/spring-context.xml" })
public class StringRedisTemplateTest {

    @Autowired
    @Qualifier("stringRedisTemplate")
    StringRedisTemplate redisTemplate;

    /**
     * Description: 展示String数据结构操作<br>
     * Create By:  <br>
     * Create Date: 2017年3月20日 上午9:06:12
     */
    @Test
    public void testValueOperations() {
        String key = "StringRedisTemplateTest:TEST:String:1";
        ValueOperations<String, String> valueOp = redisTemplate.opsForValue();
        valueOp.set(key, "1");
        Assert.assertEquals("1", valueOp.get(key));
        redisTemplate.delete(key);
        Assert.assertNull(valueOp.get(key));

    }

    /**
     * Description: 展示Set数据结构操作<br>
     * Create By:  <br>
     * Create Date: 2017年3月20日 上午9:06:12
     */
    @Test
    public void testSetOperations() {
        String key = "StringRedisTemplateTest:TEST:Set:1";
        SetOperations<String, String> op = redisTemplate.opsForSet();
        op.add(key, "1", "2");
        op.add(key, "2", "3", "4");
        op.remove(key, "4");
        Assert.assertEquals(Long.valueOf(3), op.size(key));
        redisTemplate.delete(key);
        Assert.assertEquals(Long.valueOf(0), op.size(key));

    }

    /**
     * Description: 展示SortedSet数据结构操作<br>
     * Create By:  <br>
     * Create Date: 2017年3月20日 上午9:06:12
     */
    @Test
    public void testZSetOperations() {
        String key = "StringRedisTemplateTest:TEST:ZSet:1";
        ZSetOperations<String, String> op = redisTemplate.opsForZSet();
        // 添加Key及用于排序的得分
        op.add(key, "1", 1);
        op.add(key, "2", 2);
        op.add(key, "3", 3);
        op.add(key, "4", 2);
        // 更新
        op.add(key, "4", 4);

        Assert.assertEquals(Long.valueOf(2), op.count(key, 3, 4));

        op.remove(key, "3");
        Assert.assertEquals(Long.valueOf(3), op.size(key));

        redisTemplate.delete(key);
        Assert.assertEquals(Long.valueOf(0), op.size(key));

    }

    /**
     * Description: 展示Hash数据结构操作<br>
     * Create By:  <br>
     * Create Date: 2017年3月20日 上午9:06:12
     */
    @Test
    public void testHashOperations() {
        String key = "StringRedisTemplateTest:TEST:Hash:1";
        HashOperations<String, String, String> op = redisTemplate.opsForHash();

        op.put(key, "1", "1");

        Map<String, String> map = new HashMap<String, String>();
        map.put("2", "2");
        map.put("3", "3");
        map.put("4", "4");
        map.put("5", "5");

        op.putAll(key, map);

        Assert.assertEquals(5, op.entries(key).size());

        op.delete(key, "4", "5");
        Assert.assertEquals(3, op.entries(key).size());

        redisTemplate.delete(key);
        Assert.assertEquals(0, op.entries(key).size());

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值