springboot之使用redistemplate优雅地操作redis (4)

新建一个实体类:

package com.domain; public class UserVo { public static final String Table = "t_user"; private String name; private String address; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "UserVo{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", age=" + age + '}'; } }

再新建一个测试类:

package com.config; import com.domain.UserVo; import com.service.RedisService; import com.util.RedisKeyUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.*; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.util.Set; import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest public class RedisConfigTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Resource private ValueOperations<String,Object> valueOperations; @Autowired private HashOperations<String, String, Object> hashOperations; @Autowired private ListOperations<String, Object> listOperations; @Autowired private SetOperations<String, Object> setOperations; @Autowired private ZSetOperations<String, Object> zSetOperations; @Resource private RedisService redisService; @Test public void testObj() throws Exception{ UserVo userVo = new UserVo(); userVo.setAddress("上海"); userVo.setName("测试dfas"); userVo.setAge(123); ValueOperations<String,Object> operations = redisTemplate.opsForValue(); redisService.expireKey("name",20, TimeUnit.SECONDS); String key = RedisKeyUtil.getKey(UserVo.Table,"name",userVo.getName()); UserVo vo = (UserVo) operations.get(key); System.out.println(vo); } @Test public void testValueOption( )throws Exception{ UserVo userVo = new UserVo(); userVo.setAddress("上海"); userVo.setName("jantent"); userVo.setAge(23); valueOperations.set("test",userVo); System.out.println(valueOperations.get("test")); } @Test public void testSetOperation() throws Exception{ UserVo userVo = new UserVo(); userVo.setAddress("北京"); userVo.setName("jantent"); userVo.setAge(23); UserVo auserVo = new UserVo(); auserVo.setAddress("n柜昂周"); auserVo.setName("antent"); auserVo.setAge(23); setOperations.add("user:test",userVo,auserVo); Set<Object> result = setOperations.members("user:test"); System.out.println(result); } @Test public void HashOperations() throws Exception{ UserVo userVo = new UserVo(); userVo.setAddress("北京"); userVo.setName("jantent"); userVo.setAge(23); hashOperations.put("hash:user",userVo.hashCode()+"",userVo); System.out.println(hashOperations.get("hash:user",userVo.hashCode()+"")); } @Test public void ListOperations() throws Exception{ UserVo userVo = new UserVo(); userVo.setAddress("北京"); userVo.setName("jantent"); userVo.setAge(23); // listOperations.leftPush("list:user",userVo); // System.out.println(listOperations.leftPop("list:user")); // pop之后 值会消失 System.out.println(listOperations.leftPop("list:user")); } } 注解缓存的使用

@Cacheable:在方法执行前Spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;没有则调用方法并将方法返回值放进缓存。

@CachePut:将方法的返回值放到缓存中。

@CacheEvict:删除缓存中的数据。

最后所有的代码都被上传到我的github喜欢的话,给个start

参考:
Redis 学习(二) —— 数据类型及操作
安装参考

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfgxy.html