Redis Java String(字符串) 实例
//定义User
User user = new User("aa@126.com", "aa", "aa123456", "aa", "123");
//缓存基本的对象,Integer、String、实体类等
ValueOperations<String, User> operation = redisTemplate.opsForValue();
operation.set("user_String", user);
operation.set("user_String_1seconds", user, 1, TimeUnit.SECONDS);
Thread.sleep(1000);
System.out.println(redisTemplate.opsForValue().get("user_String").toString());
boolean exists = redisTemplate.hasKey("user_String_1seconds");
if (exists) {
System.out.println("exists is true");
} else {
System.out.println("exists is false");
}
//删除key
redisTemplate.delete("user_String");
Redis Java List(列表) 实例
//缓存List数据
List<User> dataList = new ArrayList<>();
dataList.add(user);
dataList.add(user);
ListOperations listOperation = redisTemplate.opsForList();
if (null != dataList) {
int size = dataList.size();
for (int i = 0; i < size; i++) {
listOperation.leftPush("user_List", dataList.get(i));
}
}
Long size = listOperation.size("user_List");
List<Object> dataListReturn = new ArrayList<>();
for (int i = 0; i < size; i++)
{
dataListReturn.add(listOperation.index("user_List", i));
}
System.out.println(dataListReturn);
Redis Java Set(集合) 实例
//缓存Set
Set<String> dataSet = new HashSet<>();
dataSet.add("0");
dataSet.add("1");
dataSet.add("3");
dataSet.add("4");
dataSet.add("5");
BoundSetOperations<String, String> setOperation = redisTemplate.boundSetOps("user_Set");
Iterator<String> it = dataSet.iterator();
while (it.hasNext())
{
setOperation.add(it.next());
}
Set<Object> dataSetReturn = new HashSet<Object>();
dataSet = setOperation.members();
System.out.println(dataSet);
Redis Java Map 实例
//缓存Map
Map<String, User> dataMap = new HashMap<>();
dataMap.put("user",user);
HashOperations hashOperations = redisTemplate.opsForHash();
if (null != dataMap)
{
for (Map.Entry<String, User> entry : dataMap.entrySet())
{
hashOperations.put("user_Map", entry.getKey(), entry.getValue());
}
}
Map<String, User> dataMapTeturn = hashOperations.entries("user_Map");
System.out.println(dataMapTeturn);