标签:add star nbsp 存储 range members public rgs min
照着文档,找对应的函数就好了,封装得很完整,使用真不是什么难事。
至于二次封装的话,去实现Spring的Cache接口是一种方式,但是,不能把注解设计得太复杂,不爱用的话设计就失败了,需要牺牲一部分Jedis的特性,
个人推荐使用静态类,方便后期函数扩展,最大程度保证原有的功能。
package cn.swsk.xbry.api.v1.md1001; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashMap; import java.util.Map; /** * @author css * @date 2019/9/24 22:20 */ public class Test { public static void main(String[] args) throws InterruptedException { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(25); config.setMaxIdle(20); config.setMinIdle(5); try ( JedisPool pool = new JedisPool(config, "127.0.0.1", 6379); Jedis jedis = pool.getResource()) { jedis.select(0); //type of hash //1、分别给哈希表key中的域设置值,分别对key中的字段1、字段2设置值 jedis.hset("testHash" , "field1" , "value1"); jedis.hset("testHash" , "field2" , "value1"); //2、用一个JAVA中的HashMap直接存储为redis中的哈希表 Map<String, String> map = new HashMap<>(); map.put("field11" , "value11"); map.put("field12" , "value11"); jedis.hmset("testHash2" , map); System.out.println(jedis.keys("*")); System.out.println(jedis.hkeys("testHash")); System.out.println(jedis.hgetAll("testHash")); System.out.println(jedis.hgetAll("testHash2")); } } }
package cn.swsk.xbry.api.v1.md1001; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * @author css * @date 2019/9/24 22:20 */ public class Test { public static void main(String[] args) throws InterruptedException { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(25); config.setMaxIdle(20); config.setMinIdle(5); try ( JedisPool pool = new JedisPool(config, "127.0.0.1", 6379); Jedis jedis = pool.getResource()) { jedis.select(0); //type of list String key1 = "key1"; //将值value插入到列表key的表头。 jedis.lpush(key1, "what", "how", "1", "0哈哈哈哈"); //取出 System.out.println(jedis.lpop(key1)); System.out.println(jedis.lpop(key1)); System.out.println(jedis.lpop(key1)); //返回列表key中指定区间内的元素,区间以偏移量start和stop指定. 下标(index)参数start和stop从0开始;
//负数下标代表从后开始(-1表示列表的最后一个元素,-2表示列表的倒数第二个元素,以此类推) 返回一个List System.out.println(jedis.lrange(key1, 0, jedis.llen(key1) - 1)); System.out.println(jedis.lrange(key1, 0, jedis.llen(key1) - 1)); } } }
package cn.swsk.xbry.api.v1.md1001; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * @author css * @date 2019/9/24 22:20 */ public class Test { public static void main(String[] args) throws InterruptedException { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(25); config.setMaxIdle(20); config.setMinIdle(5); try ( JedisPool pool = new JedisPool(config, "127.0.0.1", 6379); Jedis jedis = pool.getResource()) { jedis.select(0); //type of set String key1 = "key3", key2 = "key2"; //将member元素加入到集合key当中。 jedis.sadd(key1, "I", "want", "to", "do", "something", "it", "with", "my", "friend"); //返回集合key的元素的数量 System.out.println(jedis.scard(key1)); //移除集合中的多个member元素。 jedis.srem(key1, "it"); System.out.println(jedis.scard(key1)); //判断元素是否是集合key的成员 System.out.println(jedis.sismember(key1, "it")); //返回集合key中的所有成员。返回一个set System.out.println(jedis.smembers(key1)); //交集 System.out.println(jedis.sinter(key1, key2)); //并集 System.out.println(jedis.sunion(key1, key2)); //差集 System.out.println(jedis.sdiff(key1, key2)); } } }
标签:add star nbsp 存储 range members public rgs min
原文地址:https://www.cnblogs.com/chenss15060100790/p/11604439.html