Spring Redis(2)数据访问
来源:互联网 发布:淘宝新店0销量 编辑:程序博客网 时间:2024/05/17 23:46
Redis数据访问
RedisTemplate
Redis主要操作接口
定义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
- Spring Redis(2)数据访问
- spring整合redis(2)spring操作redis数据
- 使用spring访问redis
- SpringBoot27-spingboot数据访问-NoSQL(Redis)
- Redis客户端访问(2)
- Spring数据访问模版
- Spring 数据访问策略
- spring boot demo(spring jdbc访问数据)
- Spring 数据访问那些事儿(一)spring + jdbc
- Spring 数据访问那些事儿(二)Spring + JdbcTemplate
- spring+redis做数据缓存
- Spring Boot Redis 数据缓存
- Spring+Redis结合(插入和提取数据)
- Spring + Hibernate 数据访问策略
- Spring + Hibernate 数据访问策略
- Spring 数据访问之事务管理
- Spring 之数据访问 JdbcTemplate
- Spring Boot的数据访问
- LSTM-tensorflow资料汇总
- vijos刷题记录(可能弃坑
- linux chattr 用法示例 修改文件属性
- 编程总结(二)C++11中的多线程
- input标签type="file"的格式要求,以及文件阅读,图片上传前的显示
- Spring Redis(2)数据访问
- 2017华为机试题--投骰子问题
- 将博客搬至CSDN
- swift navigationItem titleButton
- 栈模拟进制转化
- 双亲加载模式
- 操作系统实验四之实现shell
- classic bluetooth chat 三
- Apache Maven 入门篇(下)