Redis工具的使用和实例展示

2018/12/09 21:28
阅读数 52

*************************************Redis工具*********************************
/**
* Redis工具类
*/
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
@Resource(name = "redisTemplate")
private ValueOperations<String, String> valueOperations;
@Resource(name = "redisTemplate")
private HashOperations<String, String, Object> hashOperations;
@Resource(name = "redisTemplate")
private ListOperations<String, Object> listOperations;
@Resource(name = "redisTemplate")
private SetOperations<String, Object> setOperations;
@Resource(name = "redisTemplate")
private ZSetOperations<String, Object> zSetOperations;
/** 默认过期时长,单位:秒 */
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
/** 不设置过期时长 */
public final static long NOT_EXPIRE = -1;

public void set(String key, Object value, long expire) {
valueOperations.set(key, toJson(value));
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}

public void set(String key, Object value) {
set(key, value, DEFAULT_EXPIRE);
}

public <T> T get(String key, Class<T> clazz, long expire) {
String value = valueOperations.get(key);
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
}

public <T> T get(String key, Class<T> clazz) {
return get(key, clazz, NOT_EXPIRE);
}

public String get(String key, long expire) {
String value = valueOperations.get(key);
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value;
}

public String get(String key) {
return get(key, NOT_EXPIRE);
}

public void delete(String key) {
redisTemplate.delete(key);
}

/**
* Object转成JSON数据
*/
private String toJson(Object object) {
if (object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double || object instanceof Boolean || object instanceof String) {
return String.valueOf(object);
}
return JSON.toJSONString(object);
}

/**
* JSON数据,转成Object
*/
private <T> T fromJson(String json, Class<T> clazz) {
return JSON.parseObject(json, clazz);
}
}
**************************************实例部分************************************************
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisUtils redisUtils;

@Test
public void contextLoads() {
//生成一个对象
SysUserEntity user = new SysUserEntity();
user.setEmail("123456@qq.com");
redisUtils.set("user", user);//把对象存到redis中

redisUtils.set("zhangsan","123");//将一个字符串存到redis中

//得到redis中的对象
System.out.println(ToStringBuilder.reflectionToString(redisUtils.get("user", SysUserEntity.class)));
Date s= redisUtils.get("user",SysUserEntity.class).getCreateTime();//获取对象属性
System.out.println("s:"+s);

Map paramap=new HashMap();//存map集合
paramap.put("userName","lc");
paramap.put("password","1234");
redisUtils.set("map",paramap);


Map pp=redisUtils.get("map",Map.class);//获取map集合
System.out.println("password"+pp.get("password"));//获取map集合中存入的数据
System.out.println(redisUtils.get("map"));

System.out.println(redisUtils.get("zhangsan"));//从redis中获取zhangsan对象


redisUtils.delete("zhangsan");//从redis中删除一个对象
System.out.println(redisUtils.get("zhangsan"));

redisUtils.set("lisi","1234",5);//向redis中设置一个字符串,并且设置过期时间为5秒中
System.out.println(redisUtils.get("lisi"));

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(redisUtils.get("lisi"));

}



展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部