写在前面
Jedis
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.set("name","wangwu");
String name = jedis.get("name");
System.out.println(name);
jedis.close();
hash
HashMap<String, String> map = new HashMap<>();
map.put("name","zhangsan");
map.put("age","18");
map.put("weight","68.7");
jedis.hmset("hash",map);
Map<String, String> hash = jedis.hgetAll("hash");
hash.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String s, String s2) {
System.out.println(s + "---" + s2);
}
});
list
jedis.lpush("list", "a","b","c");
jedis.rpush("list", "a","b","c");
List<String> list = jedis.lrange("list", 0, -1);
for (String s : list) {
System.out.println(s);
}
封装工具类
public class JedisUtils {
private static String host
private static int port
private static int maxTotal
private static int maxIdle
private static JedisPool jp = null
static {
ResourceBundle redis = ResourceBundle.getBundle("redis")
host = redis.getString("redis.host")
port = Integer.parseInt(redis.getString("redis.port"))
maxTotal = Integer.parseInt(redis.getString("redis.maxTotal"))
maxIdle = Integer.parseInt(redis.getString("redis.maxIdle"))
JedisPoolConfig jpc = new JedisPoolConfig()
jpc.setMaxTotal(maxTotal)
//最大空闲数
jpc.setMaxIdle(maxIdle)
jp = new JedisPool(jpc, host, port)
}
public static Jedis getJedis() {
return jp.getResource()
}
}
RedisTemplate
- 基于spring-boot,需要导包和配置
- 命令与控制台差不多,下面仅作部分演示
- 需要配置序列化格式,否则会有前缀
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
string
- 对String的操作直接使用
StringRedisTemplate
stringRedisTemplate.delete("name");
stringRedisTemplate.boundValueOps("name").set("zhangsan");
stringRedisTemplate.opsForValue().set("age","18");
String name = stringRedisTemplate.boundValueOps("name").get();
stringRedisTemplate.boundValueOps("age").increment(2L);
stringRedisTemplate.boundValueOps("weight").set("80",10L, TimeUnit.SECONDS);
stringRedisTemplate.boundValueOps("name").append("dwadaw");
hash
HashMap<String, Object> user = new HashMap<>()
user.put("name","zhangsan")
user.put("age",20)
user.put("weight",63)
//put
redisTemplate.boundHashOps("user").putAll(user)
//获取所有字段
Set keys = redisTemplate.boundHashOps("user").keys()
System.out.println(keys)
//获取所有值
List values = redisTemplate.boundHashOps("user").values()
System.out.println(values)
//键值对
Map entries = redisTemplate.boundHashOps("user").entries()
System.out.println(entries)
//删除
redisTemplate.boundHashOps("user").delete("weight")
//是否存在
Boolean res = redisTemplate.boundHashOps("user").hasKey("weight")
System.out.println(res)
//put单个
redisTemplate.boundHashOps("user").put("weight",300)
list
BoundListOperations listKey = redisTemplate.boundListOps("listKey")
//push
listKey.leftPushAll("zhangsan","lisi", "wangwu")
listKey.rightPushAll("zhangsan","lisi","wangwu")
//获取
List list = listKey.range(0, -1)
System.out.println(list)
List list1 = redisTemplate.opsForList().range("listKey", 0,-1)
System.out.println(list1)
//pop
Object o = listKey.leftPop()
System.out.println(o)
Object o1 = listKey.rightPop()
System.out.println(o1)
//10秒之内存储数据
Object o2 = listKey.leftPop(10L, TimeUnit.SECONDS)
System.out.println(o2)
//删除指定索引的数量
listKey.remove(1,"zhangsan")
//根据索引设置
listKey.set(3,"xiaoming")
//根据索引值获取数据
Object index = listKey.index(0)
System.out.println(index)
set
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey")
//add
setKey.add("name","age","weight")
//获取
Set members = setKey.members()
System.out.println(members)
//size
Long size = setKey.size()
System.out.println(size)
//是否存在
Boolean res = setKey.isMember("addr")
System.out.println(res)
//移除
setKey.remove("name","age")
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
Set res = setKey.union("setKey2");
setKey.unionAndStore("setKey2","setKey3");
redisTemplate.opsForSet().unionAndStore("setKey","setKey2","setKey4");
HashSet<String> hashSet = new HashSet<>();
hashSet.add("setKey4");
hashSet.add("setKey3");
setKey.unionAndStore(hashSet,"setKey5");
sorted_set
//一个个添加
BoundZSetOperations zsetKey = redisTemplate.boundZSetOps("zsetKey")
zsetKey.add("zhangsan",80)
zsetKey.add("lisi",100)
zsetKey.add("wangwu",90)
//添加多个 DefaultTypedTuple zset的数据类型
DefaultTypedTuple<String> p1 = new DefaultTypedTuple("wangwu1", 65.0)
DefaultTypedTuple<String> p2 = new DefaultTypedTuple("wangwu2", 50.0)
DefaultTypedTuple<String> p3 = new DefaultTypedTuple("wangwu3", 40.0)
zsetKey.add(new HashSet<>(Arrays.asList(p1,p2,p3)))
//升序输出
Set range = zsetKey.range(0, -1)
System.out.println(range)
//降序输出
Set set1 = zsetKey.reverseRange(0, -1)
System.out.println(set1)
//min max 升序
Set set = zsetKey.rangeByScore(75, 95)
System.out.println(set)
//min max 降序
Set set2 = zsetKey.reverseRangeByScore(75, 95)
System.out.println(set2)
//获取分数范围内的
Set<DefaultTypedTuple<String>> tuples = zsetKey.rangeWithScores(0, -1)
for (DefaultTypedTuple<String> tuple : tuples) {
System.out.println(tuple.getValue() + " - " + tuple.getScore())
}
//获取指定
Double resLisi = zsetKey.score("lisi")
System.out.println(resLisi)
//获取数量
Long count = zsetKey.count(75, 95)
System.out.println(count)
//移除指定
zsetKey.remove("wangwu3")
//按照升序的顺序删除,删除成绩最低的三个
zsetKey.removeRange(0,3)
//分数范围内移除
zsetKey.removeRangeByScore(30,60)