//处理乱码:采用了String的序列化策略,只接受value值类型为String的参数
@Configuration
public class RedisCacheClient extends RedisTemplate<String, String> {
private RedisCacheClient() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
this.setKeySerializer(stringSerializer);
this.setValueSerializer(stringSerializer);
this.setHashKeySerializer(stringSerializer);
this.setHashValueSerializer(stringSerializer);
}
public RedisCacheClient(final RedisConnectionFactory connectionFactory) {
this();
this.setConnectionFactory(connectionFactory);
this.afterPropertiesSet();
}
@Override
@NonNull
protected RedisConnection preProcessConnection(@NonNull final RedisConnection connection, final boolean existingConnection) {
return new DefaultStringRedisConnection(connection);
}
}
@Test
public void test3() {
//赋值
ValueOperations ops = redisCacheClient.opsForValue();
ops.set("Tom", "18");
ops.set("Dick", "20", 1, TimeUnit.MINUTES);
//获取值
Object stringKey = ops.get("Tom");
System.out.println(stringKey);
//删除
redisCacheClient.delete("Tom");
List<String> strings = Arrays.asList("Tom", "Dick");
redisCacheClient.delete(strings);
//判断是否存在
oolean tom = redisCacheClient.hasKey("Tom");
}
@Test
public void test3() {
redisCacheClient.boundZSetOps("三国2").add("曹操",1);
redisCacheClient.boundZSetOps("三国2").add("曹操2",2);
redisCacheClient.boundZSetOps("三国2").add("曹操3",3);
redisCacheClient.boundZSetOps("三国2").add("曹操4",4);
redisCacheClient.boundZSetOps("三国2").add("曹操5",5);
//按照分数正序排序
redisCacheClient.opsForZSet().rangeByScoreWithScores("三国2", 1, 50, 0, 3);
//按照分数倒序排序
redisCacheClient.opsForZSet().reverseRangeByScoreWithScores("三国2", 1, 50, 0, 3);
//判断key中是否存在value值
redisCacheClient.opsForSet().isMember(key,value);
redisCacheClient.boundSetOps("三国").add("刘备");
redisCacheClient.boundSetOps("三国").add("孙权");
redisCacheClient.boundSetOps("三国").members();
redisCacheClient.boundSetOps("三国").remove("孙权");
redisCacheClient.delete("三国");
redisCacheClient.boundListOps("桃园三结义").rightPush("刘备");
redisCacheClient.boundListOps("桃园三结义").rightPush("关羽");
redisCacheClient.boundListOps("桃园三结义").rightPush("张飞");
redisCacheClient.boundListOps("桃园三结义").range(0, 10);
redisCacheClient.boundListOps("桃园三结义").index(1);
redisCacheClient.boundListOps("桃园三结义").remove(1, "关羽");
redisCacheClient.boundHashOps("西游记").put("老大", "唐僧");
redisCacheClient.boundHashOps("西游记").put("老二", "悟空");
redisCacheClient.boundHashOps("西游记").put("老三", "八戒");
redisCacheClient.boundHashOps("西游记").put("老四", "沙僧");
redisCacheClient.boundHashOps("西游记").keys();
redisCacheClient.boundHashOps("西游记").values();
redisCacheClient.boundHashOps("西游记").get("老四");
redisCacheClient.boundHashOps("西游记").delete("c", "a");
redisCacheClient.boundHashOps("西游记").delete();
}
opsForXXX和boundXXXOps的区别?
XXX为value的类型,前者获取一个operator,但是没有指定操作的对象(key),可以在一个连接(事务)内操作多个key以及对应的value;后者获取了一个指定操作对象(key)的operator,在一个连接(事务)内只能操作这个key对应的value。
关于计数的API(increment)有一个bug,需要各位使用中注意,通过increment计数以后,通过get方式获取计数值的时候可能会抛出EOF异常(和本地的jdk以及redis的编译版本有关),可以考虑使用boundValueOps(key).get(0,-1)获取计数值。