Spring Redis(2)数据访问

来源:互联网 发布:淘宝新店0销量 编辑:程序博客网 时间:2024/05/17 23:46

Redis数据访问

RedisTemplate

Redis主要操作接口
接口 作用 基本类型操作 - ValueOperations Redis string操作 ListOperations Redis list 操作 SetOperations Redis set 操作 ZSetOperations Redis zset 操作 HashOperations Redis hash 操作 HyperLogLogOperations Redis HyperLogLog operations like (pfadd, pfcount,…​) GeoOperations Redis geospatial operations like GEOADD, GEORADIUS,…​) 批量操作 - BoundValueOperations Redis string (or value) key批量操 BoundListOperations Redis list key 批量操 BoundSetOperations Redis set key 批量操 BoundZSetOperations Redis zset (or sorted set) key 批量操 BoundHashOperations Redis hash key 批量操 BoundGeoOperations Redis 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());    }}
0 0
原创粉丝点击