在springboot环境下连接redis的方法有很多,首先最简单的就是直接通过jedis类来连接,jedis类就相当于是redis的客户端表示。
但是因为现在比较常用的是:StringRedisTemplate和RedisTemplate,所以jedis只做简单的介绍。
一、Jedis连接方式
1) Jedis直接链接Redis服务器
第一步:先引入Jedis的依赖:
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.11.0</version>
</dependency>
第二步 直接连接:
public static void main(String[] args) {//连接本地的 Redis 服务Jedis jedis = new Jedis("localhost",6379);// 如果 Redis 服务设置了密码,需要用下面这行代码输入密码jedis.auth("123456");System.out.println("连接成功");//查看服务是否运行System.out.println("服务正在运行: "+jedis.ping());}
显示已经连接成功!
2)通过连接池的方式获取 Jedis
第一步仍然是引入依赖。
第二步代码实现:
public static void main(String[] args) {// 创建Jedis连接池配置对象JedisPoolConfig config = new JedisPoolConfig();// 最大连接数config.setMaxTotal(30);// 最大空闲连接数config.setMaxIdle(10);// 最小空闲连接数config.setMinIdle(5);// 最长等待毫秒数config.setMaxWaitMillis(3000);// 连接到 redis 服务器上JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),"localhost", 6379, 10000, "123456");for (int i = 1; i <=31; i++) {Jedis jedis = jedisPool.getResource();System.out.println(jedis);
// jedis.close(); // 将连接归还到连接池}jedisPool.close(); // 释放资源}
需要先创建JedisPoolConfig 连接池配置对象来进行配置,然后再使用JedisPool 进行连接。
3)使用工厂模式连接Redis
一般我们不会这个频繁的创建Redis连接,正常情况下会使用工厂模式进行创建,这样会方便很多。
1)首先创建配置文件,将Jedis的各种配置信息,全都放到配置文件中设置
redis:host: localhostport: 6379database: 1#连接超时时间timeout: 10password: 123456#最大连接数poolMaxTotal=: 10#最大空闲连接数poolMaxIdle: 5#最大等待连接数poolMaxWait: 3
2)创建配置类:
@Component
@ConfigurationProperties(prefix = "redis")
@Data
public class RedisConfig {private String host;private int port;private int timeout;//秒private String password;private int poolMaxTotal;private int poolMaxIdle;private int poolMaxWait;//秒private int database;
}
@Configuration
public class JedisPoolFactory {@AutowiredRedisConfig redisConfig;@Beanpublic JedisPool JedisPoolFactory(){System.out.println(redisConfig.toString());JedisPoolConfig poolConfig=new JedisPoolConfig();poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000);JedisPool jp=new JedisPool(poolConfig,redisConfig.getHost(),redisConfig.getPort(),redisConfig.getTimeout()*1000,redisConfig.getPassword(),redisConfig.getDatabase());return jp;}
}
3)像上面创建完配置类之后就可以直接使用Jedis了,示例如下:
@RestController
@RequestMapping("/redis")
public class RedisController {@AutowiredJedisPool jedisPool;@GetMapping("/demo")public void name(@RequestParam("name") String name){Jedis jedis = jedisPool.getResource();jedis.set("name", name);jedis.close();}
}
这是最简单的使用示例。
4) 哨兵模式连接Redis
1.首先还是要创建配置文件跟配置类,只不过属性要稍微改一下
redis:sentinelHost: localhost1:26379,localhost2:localhost:26379masterName: mymaster
# host: host
# port: 6379database: 1#连接超时时间timeout: 30password: 123456#最大连接数poolMaxTotal=: 10#最大空闲连接数poolMaxIdle: 5#最大等待连接数poolMaxWait: 3
配置类:
@Component
@ConfigurationProperties(prefix = "redis")
@Data
public class RedisConfig {private String sentinelHost;private String masterName;
// private int port;private int timeout;//秒private String password;private int poolMaxTotal;private int poolMaxIdle;private int poolMaxWait;//秒private int database;}
2.创建连接池:
@Configuration
public class JedisPoolFactory {@AutowiredRedisConfig redisConfig;@Beanpublic JedisSentinelPool JedisPoolFactory(){System.out.println(redisConfig.toString());JedisPoolConfig poolConfig=new JedisPoolConfig();poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000);// 哨兵信息,注意填写哨兵的地址String[] host = redisConfig.getSentinelHost().split(",");Set<String> sentinels = new HashSet<>(Arrays.asList(host));// 创建连接池JedisSentinelPool jp = new JedisSentinelPool(redisConfig.getMasterName(),sentinels,poolConfig,2000, redisConfig.getPassword(),redisConfig.getDatabase());return jp;}
}
4) 集群模式连接:
都差不多,只简单说一下如何创建连接池:
public static void testCluster() {// 配置参数,根据服务器情况设置连接参数JedisPoolConfig config = new JedisPoolConfig();config.setMaxIdle(100);// 添加集群master地址Set<HostAndPort> jedisClusterSet = new HashSet<>();jedisClusterSet.add(new HostAndPort("192.168.242.128", 8201));jedisClusterSet.add(new HostAndPort("192.168.242.128", 8202));jedisClusterSet.add(new HostAndPort("192.168.242.128", 8203));// 连接集群JedisCluster jedisCluster = new JedisCluster(jedisClusterSet, config);// r如果带密码//JedisCluster jedisCluster = new JedisCluster(jedisClusterSet,1000, 1000, 5, password, config);// 设置keyjedisCluster.set("okayjam", "www.okayjam.com");// 读取keySystem.out.println(jedisCluster.get("okayjam"));
}
二、Jedis的操作:
1)Jedis对key的操作
jedis.flushDB();// 清空数据jedis.echo("hello");// 判断key否存在jedis.exists("foo");// 如果数据库没有任何key,返回nil,否则返回数据库中一个随机的key。String randomKey = jedis.randomKey();System.out.println("randomKey: " + randomKey);// 设置60秒后该key过期jedis.expire("key", 60);// key有效毫秒数System.out.println(jedis.pttl("key"));// 移除key的过期时间jedis.persist("key");// 获取key的类型, "string", "list", "set". "none" none表示key不存在System.out.println("type: " + jedis.type("key"));// 导出key的值byte[] bytes = jedis.dump("key");System.out.println(new String(bytes));// 将key重命名jedis.renamenx("key", "keytest");System.out.println("key是否存在: " + jedis.exists("key"));// 判断是否存在System.out.println("keytest是否存在: " + jedis.exists("keytest"));// 判断是否存在// 查询匹配的key// KEYS * 匹配数据库中所有 key 。// KEYS h?llo 匹配 hello , hallo 和 hxllo 等。// KEYS h*llo 匹配 hllo 和 heeeeello 等。// KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。// 特殊符号用 \ 隔开。Set<String> set = jedis.keys("k*");System.out.println(set);// 删除keyjedis.del("key");System.out.println(jedis.exists("key"));
2) Jedis对字符串(String)的相关操作
jedis.set("hello", "hello");// 使用append 向字符串后面添加jedis.append("hello", " world");System.out.println(jedis.get("hello"));// set覆盖字符串jedis.set("hello", "123");System.out.println(jedis.get("hello"));// 设置过期时间jedis.setex("hello2", 2, "world2");System.out.println(jedis.get("hello2"));// 一次添加多个key-value对jedis.mset("a", "1", "b", "2");// 获取a和b的valueList<String> valus = jedis.mget("a", "b");System.out.println(valus);// 批量删除jedis.del("a", "b");System.out.println(jedis.exists("a"));System.out.println(jedis.exists("b"));
3)Jedis对链表(Lists)的操作
String key = "mylist";jedis.del(key);// 队列添加元素jedis.rpush(key, "aaaa");jedis.rpush(key, "aaaa");jedis.rpush(key, "bbbb");jedis.rpush(key, "cccc");jedis.rpush(key, "cccc");// 队列长度System.out.println("lenth: " + jedis.llen(key));// 打印队列,从索引0开始,到倒数第1个(全部元素)System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 索引为1的元素System.out.println("index of 1: " + jedis.lindex(key, 1));// 设置队列里面一个元素的值,当index超出范围时会返回一个error。jedis.lset(key, 1, "aa22");System.out.println("index of 1: " + jedis.lindex(key, 1));// 从队列的右边入队一个元素jedis.rpush(key, "-2", "-1");// 先-2,后-1入队列System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 从队列的左边入队一个或多个元素jedis.lpush(key, "second element", "first element");// 先second// element,后first// elementF入队列System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 从队列的右边出队一个元素System.out.println(jedis.rpop(key));// 从队列的左边出队一个元素System.out.println(jedis.lpop(key));System.out.println("all elements: " + jedis.lrange(key, 0, -1));// count > 0: 从头往尾移除值为 value 的元素,count为移除的个数。// count < 0: 从尾往头移除值为 value 的元素,count为移除的个数。// count = 0: 移除所有值为 value 的元素。jedis.lrem(key, 1, "cccc");System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 即最右边的那个元素也会被包含在内。 如果start比list的尾部下标大的时候,会返回一个空列表。// 如果stop比list的实际尾部大的时候,Redis会当它是最后一个元素的下标。System.out.println(jedis.lrange(key, 0, 2));System.out.println("all elements: " + jedis.lrange(key, 0, -1));// 删除区间以外的元素System.out.println(jedis.ltrim(key, 0, 2));System.out.println("all elements: " + jedis.lrange(key, 0, -1));
4)Jedis对集合(Sets)的操作
// 清空数据System.out.println(jedis.flushDB());String key = "myset";String key2 = "myset2";// 集合添加元素jedis.sadd(key, "aaa", "bbb", "ccc");jedis.sadd(key2, "bbb", "ccc", "ddd");// 获取集合里面的元素数量System.out.println(jedis.scard(key));// 获得两个集合的交集,并存储在一个关键的结果集jedis.sinterstore("destination", key, key2);System.out.println(jedis.smembers("destination"));// 获得两个集合的并集,并存储在一个关键的结果集jedis.sunionstore("destination", key, key2);System.out.println(jedis.smembers("destination"));// key集合中,key2集合没有的元素,并存储在一个关键的结果集jedis.sdiffstore("destination", key, key2);System.out.println(jedis.smembers("destination"));// 确定某个元素是一个集合的成员System.out.println(jedis.sismember(key, "aaa"));// 从key集合里面随机获取一个元素System.out.println(jedis.srandmember(key));// aaa从key移动到key2集合jedis.smove(key, key2, "aaa");System.out.println(jedis.smembers(key));System.out.println(jedis.smembers(key2));// 删除并获取一个集合里面的元素System.out.println(jedis.spop(key));// 从集合里删除一个或多个元素jedis.srem(key2, "ccc", "ddd");System.out.println(jedis.smembers(key2));
5)Jedis对有序集合(Sorted Sets)的操作
// 清空数据System.out.println(jedis.flushDB());String key = "mysortset";Map<String, Double> scoreMembers = new HashMap<String, Double>();scoreMembers.put("aaa", 1001.0);scoreMembers.put("bbb", 1002.0);scoreMembers.put("ccc", 1003.0);// 添加数据jedis.zadd(key, 1004.0, "ddd");jedis.zadd(key, scoreMembers);// 获取一个排序的集合中的成员数量System.out.println(jedis.zcard(key));// 返回的成员在指定范围内的有序集合,以0表示有序集第一个成员,以1表示有序集第二个成员,以此类推。// 负数下标,以-1表示最后一个成员,-2表示倒数第二个成员Set<String> coll = jedis.zrange(key, 0, -1);System.out.println(coll);// 返回的成员在指定范围内的逆序集合coll = jedis.zrevrange(key, 0, -1);System.out.println(coll);// 元素下标System.out.println(jedis.zscore(key, "bbb"));// 删除元素System.out.println(jedis.zrem(key, "aaa"));System.out.println(jedis.zrange(key, 0, -1));// 给定值范围内的成员数System.out.println(jedis.zcount(key, 1002.0, 1003.0));
6)Jedis对哈希(Hashs)的操作
// 清空数据System.out.println(jedis.flushDB());String key = "myhash";Map<String, String> hash = new HashMap<String, String>();hash.put("aaa", "11");hash.put("bbb", "22");hash.put("ccc", "33");// 添加数据jedis.hmset(key, hash);jedis.hset(key, "ddd", "44");// 获取hash的所有元素(key值)System.out.println(jedis.hkeys(key));// 获取hash中所有的key对应的value值System.out.println(jedis.hvals(key));// 获取hash里所有元素的数量System.out.println(jedis.hlen(key));// 获取hash中全部的域和值,以Map<String, String> 的形式返回Map<String, String> elements = jedis.hgetAll(key);System.out.println(elements);// 判断给定key值是否存在于哈希集中System.out.println(jedis.hexists(key, "bbb"));// 获取hash里面指定字段对应的值System.out.println(jedis.hmget(key, "aaa", "bbb"));// 获取指定的值System.out.println(jedis.hget(key, "aaa"));// 删除指定的值System.out.println(jedis.hdel(key, "aaa"));System.out.println(jedis.hgetAll(key));// 为key中的域 field 的值加上增量 incrementSystem.out.println(jedis.hincrBy(key, "bbb", 100));System.out.println(jedis.hgetAll(key));
三、Jedis操作封装类
一般不直接从Jedis连接池中获取实例然后进行操作,一般会写好一个封装类,然后使用封装类进行操作,简单提供几个示例:
1) 示例一
例如平时常用的redis操作的get、set、exists、incr、decr方法,序列化的beanToString和stringToBean方法,用到的是import com.alibaba.fastjson.JSON;
@Service
public class RedisService {@AutowiredJedisPool jedisPool;/*** 获取单个对象* @param prefix* @param key* @param clazz* @param <T>* @return*/public <T> T get(KeyPrefix prefix,String key,Class<T> clazz){Jedis jedis =null;try {jedis = jedisPool.getResource();//生成真正的keyString realKey =prefix.getPrefix()+key;String str = jedis.get(realKey);T t=stringToBean(str,clazz);return t;}finally {returnToPool(jedis);}}/*** 设置对象* @param prefix* @param key* @param value* @param <T>* @return*/public <T> boolean set(KeyPrefix prefix,String key,T value){Jedis jedis =null;try {jedis = jedisPool.getResource();String str=beanToString(value);if (str==null||str.length()<=0)return false;//生成真正的keyString realKey =prefix.getPrefix()+key;int seconds=prefix.expireSeconds();if (seconds<=0){jedis.set(realKey,str);} else {jedis.setex(realKey,seconds,str);}jedis.set(realKey, str);return true;}finally {returnToPool(jedis);}}/*** 判断是否存在* @param prefix* @param key* @param <T>* @return*/public <T> boolean exists(KeyPrefix prefix,String key){Jedis jedis =null;try {jedis = jedisPool.getResource();//生成真正的keyString realKey =prefix.getPrefix()+key;return jedis.exists(realKey);}finally {returnToPool(jedis);}}/*** 增加值* @param prefix* @param key* @param <T>* @return*/public <T> Long incr(KeyPrefix prefix,String key){Jedis jedis =null;try {jedis = jedisPool.getResource();//生成真正的keyString realKey =prefix.getPrefix()+key;return jedis.incr(realKey);}finally {returnToPool(jedis);}}/*** 减少值* @param prefix* @param key* @param <T>* @return*/public <T> Long decr(KeyPrefix prefix,String key){Jedis jedis =null;try {jedis = jedisPool.getResource();//生成真正的keyString realKey =prefix.getPrefix()+key;return jedis.decr(realKey);}finally {returnToPool(jedis);}}private <T> String beanToString(T value) {if (value==null)return null;Class<?> aClass = value.getClass();if (aClass==int.class||aClass==Integer.class){return ""+value;}else if (aClass==String.class){return (String) value;}else if (aClass==long.class||aClass==Long.class){return ""+value;}else {return JSON.toJSONString(value);}}private <T> T stringToBean(String str,Class<T> aClass) {if (str==null||str.length()<=0||aClass==null)return null;if (aClass==int.class||aClass==Integer.class){return (T)Integer.valueOf(str);}else if (aClass==String.class){return (T)str;}else if (aClass==long.class||aClass==Long.class){return (T)Long.valueOf(str);}else {return JSON.toJavaObject(JSON.parseObject(str),aClass);}}private void returnToPool(Jedis jedis) {if (jedis!=null){jedis.close();}}}
2) 示例二:
/*** 基于jedis的redis操作工具类* @author jyf* @time 2019/8/3 22:30*/
public final class RedisUtils {/*除了该工具类提供的方法外,还可以在外面调用getJedis()方法,获取到jedis实例后,调用它原生的api来操作*//*** 获取jedis对象,并选择redis库。jedis默认是0号库,可传入1-16之间的数选择库存放数据* 原则上使用一个redis库存放数据,通过特定的key的命令规则来区分不同的数据就行了。** @param index redis库号。使用可变参数的目的就是该参数可传可不传。* @return 返回jedis对象*/public static Jedis getJedis(int... index) {Jedis jedis = MyJedisPoolConfig.getJedisPool().getResource();if (index != null && index.length > 0) {if (index[0] > 0 && index[0] <= 16){jedis.select(index[0]);}}return jedis;}/*######################## key的操作 ################################*//*** 根据pattern返回当前库中的key** @param pattern* @return*/public static Set<String> keys(String pattern) {Jedis jedis = null;Set<String> keys = null;try {jedis = getJedis();keys = jedis.keys(pattern);}finally {if (jedis != null){jedis.close();}}return keys;}/*** 删除一个或多个key** @param key 一个或多个key*/public static Long del(String... key) {Jedis jedis = null;Long delNum = 0L;try {jedis = getJedis();delNum =jedis.del(key);}finally {if (jedis != null){jedis.close();}}return delNum;}/*** 批量删除* @param keyList 要删除的key的集合*/public static void mdel(List<String> keyList){Jedis jedis = getJedis();//获取pipelinePipeline pipeline = jedis.pipelined();for (String key : keyList) {pipeline.del(key);}//执行结果同步,这样才能保证结果的正确性。实际上不执行该方法也执行了上面的命令,但是结果确不一定完全正确。//注意pipeline.sync();//关闭连接jedis.close();}/*** 判断某个key是否还存在** @param key key* @return*/public static Boolean exists(String key) {Jedis jedis = null;Boolean flag = false;try {jedis = getJedis();flag = jedis.exists(key);}finally {if (jedis != null){jedis.close();}}return flag;}/*** 设置某个key的过期时间,单位秒** @param key key* @param seconds 过期时间秒*/public static void expire(String key, int seconds) {Jedis jedis = null;try {jedis = getJedis();jedis.expire(key, seconds);}finally {if (jedis != null){jedis.close();}}}/*** 查看某个key还有几秒过期,-1表示永不过期 ,-2表示已过期** @param key key* @return*/public static Long timeToLive(String key) {Jedis jedis = null;Long ttl;try {jedis = getJedis();ttl = jedis.ttl(key);}finally {if (jedis != null){jedis.close();}}return ttl;}/*** 查看某个key对应的value的类型** @param key* @return*/public static String type(String key) {Jedis jedis = null;String type = null;try {jedis = getJedis();type = jedis.type(key);}finally {if (jedis != null){jedis.close();}}return type;}/*######################## string(字符串)的操作 ####################*//*** 获取某个key的value,类型要对,只能value是string的才能获取** @param key* @return*/public static String get(String key) {Jedis jedis = null;String value = null;try {jedis = getJedis();value = jedis.get(key);}finally {if (jedis != null){jedis.close();}}return value;}/*** 设置某个key的value** @param key* @param value*/public static void set(String key, String value) {Jedis jedis = null;try {jedis = getJedis();jedis.set(key, value);}finally {if (jedis != null){jedis.close();}}}/*** 字符串后追加内容** @param key key* @param appendContent 要追加的内容*/public static void append(String key, String appendContent) {Jedis jedis = null;try {jedis = getJedis();jedis.append(key, appendContent);}finally {if (jedis != null){jedis.close();}}}/*** 返回key的value的长度** @param key* @return*/public static Long strlen(String key) {Jedis jedis = null;Long strLen = 0L;try {jedis = getJedis();strLen = jedis.strlen(key);}finally {if (jedis != null){jedis.close();}}return strLen;}/*** value 加1 必* 须是字符型数字* @param key* @return 增加后的值*/public static Long incr(String key) {Jedis jedis = null;Long incrResult = 0L;try {jedis = getJedis();incrResult = jedis.incr(key);}finally {if (jedis != null){jedis.close();}}return incrResult;}/*** value 减1 必须是字符型数字** @param key* @return*/public static Long decr(String key) {Jedis jedis = null;Long decrResult = 0L;try {jedis = getJedis();decrResult = jedis.decr(key);}finally {if (jedis != null){jedis.close();}}return decrResult;}/*** value 加increment** @param key key* @param increment 加几* @return*/public static Long incrby(String key, int increment) {Jedis jedis = null;Long incrByResult = 0L;try {jedis = getJedis();incrByResult = jedis.incrBy(key, increment);}finally {if (jedis != null){jedis.close();}}return incrByResult;}/*** value 减increment** @param key* @param increment* @return*/public static Long decrby(String key, int increment) {Jedis jedis = null;Long decrByResult = 0L;try {jedis = getJedis();decrByResult = jedis.decrBy(key, increment);}finally {if (jedis != null){jedis.close();}}return decrByResult;}/*** 给某个key设置过期时间和value,成功返回OK** @param key key* @param seconds 过期时间秒* @param value 设置的值* @return*/public static String setex(String key, int seconds, String value) {Jedis jedis = null;String result = null;try {jedis = getJedis();result = jedis.setex(key, seconds, value);}finally {if (jedis != null){jedis.close();}}return result;}/*######################## list(列表)的操作 #######################*///lpush rpush lpop rpop lrange lindex llen lset/*** 从左边向列表中添加值** @param key key* @param str 要添加的值*/public static void lpush(String key, String str) {Jedis jedis = null;try {jedis = getJedis();jedis.lpush(key, str);}finally {if (jedis != null){jedis.close();}}}/*** 从右边向列表中添加值** @param key key* @param str 要添加的值*/public static void rpush(String key, String str) {Jedis jedis = null;try {jedis = getJedis();jedis.rpush(key, str);}finally {if (jedis != null){jedis.close();}}}/*** 从左边取出一个列表中的值** @param key* @return*/public static String lpop(String key) {Jedis jedis = null;String lpop = null;try {jedis = getJedis();lpop = jedis.lpop(key);}finally {if (jedis != null){jedis.close();}}return lpop;}/*** 从右边取出一个列表中的值** @param key* @return*/public static String rpop(String key) {Jedis jedis = null;String rpop = null;try {jedis = getJedis();rpop = jedis.rpop(key);}finally {if (jedis != null){jedis.close();}}return rpop;}/*** 取出列表中指定范围内的值,0 到 -1 表示全部** @param key* @param startIndex* @param endIndex* @return*/public static List<String> lrange(String key, int startIndex, int endIndex) {Jedis jedis = null;List<String> result = null;try {jedis = getJedis();result = jedis.lrange(key, startIndex, endIndex);}finally {if (jedis != null){jedis.close();}}return result;}/*** 返回某列表指定索引位置的值** @param key 列表key* @param index 索引位置* @return*/public static String lindex(String key, int index) {Jedis jedis = null;String lindex = null;try {jedis = getJedis();lindex = jedis.lindex(key, index);}finally {if (jedis != null){jedis.close();}}return lindex;}/*** 返回某列表的长度** @param key* @return*/public static Long llen(String key) {Jedis jedis = null;Long llen = 0L;try {jedis = getJedis();llen = jedis.llen(key);}finally {if (jedis != null){jedis.close();}}return llen;}/*** 给某列表指定位置设置为指定的值** @param key* @param index* @param str*/public static void lset(String key, Long index, String str) {Jedis jedis = null;try {jedis = getJedis();jedis.lset(key, index, str);}finally {if (jedis != null){jedis.close();}}}/*** 对列表进行剪裁,保留指定闭区间的元素(索引位置也会重排)* @param key 列表key* @param startIndex 开始索引位置* @param endIndex 结束索引位置*/public static void ltrim(String key,Integer startIndex,Integer endIndex){Jedis jedis = null;try {jedis = getJedis();jedis.ltrim(key, startIndex, endIndex);}finally {if (jedis != null){jedis.close();}}}/*** 从列表的左边阻塞弹出一个元素* @param key 列表的key* @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞* @return*/public static List<String> blpop(String key,Integer timeout){Jedis jedis = null;List<String> valueList = null;try {jedis = getJedis();valueList = jedis.blpop(timeout, key);}finally {if (jedis != null){jedis.close();}}return valueList;}/*** 从列表的右边阻塞弹出一个元素* @param key 列表的key* @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞* @return*/public static List<String> brpop(String key,Integer timeout){Jedis jedis = null;List<String> valueList = null;try {jedis = getJedis();valueList = jedis.brpop(timeout, key);}finally {if (jedis != null){jedis.close();}}return valueList;}/*######################## hash(哈希表)的操作 #######################*///hset hget hmset hmget hgetall hdel hkeys hvals hexists hincrby/*** 给某个hash表设置一个键值对** @param key* @param field* @param value*/public static void hset(String key, String field, String value) {Jedis jedis = null;try {jedis = getJedis();jedis.hset(key, field, value);}finally {if (jedis != null){jedis.close();}}}/*** 取出某个hash表中某个field对应的value** @param key key* @param field field* @return*/public static String hget(String key, String field) {Jedis jedis = null;String hget = null;try {jedis = getJedis();hget = jedis.hget(key, field);}finally {if (jedis != null){jedis.close();}}return hget;}/*** 某个hash表设置一个或多个键值对** @param key* @param kvMap*/public static void hmset(String key, Map<String, String> kvMap) {Jedis jedis = null;try {jedis = getJedis();jedis.hmset(key, kvMap);}finally {if (jedis != null){jedis.close();}}}/*** 取出某个hash表中任意多个key对应的value的集合** @param key* @param fields* @return*/public static List<String> hmget(String key, String... fields) {Jedis jedis = null;List<String> hmget = null;try {jedis = getJedis();hmget = jedis.hmget(key, fields);}finally {if (jedis != null){jedis.close();}}return hmget;}/*** 取出某个hash表中所有的键值对** @param key* @return*/public static Map<String, String> hgetall(String key) {Jedis jedis = null;Map<String, String> kvMap = null;try {jedis = getJedis();kvMap = jedis.hgetAll(key);}finally {if (jedis != null){jedis.close();}}return kvMap;}/*** 判断某个hash表中的某个key是否存在** @param key* @param field* @return*/public static Boolean hexists(String key, String field) {Jedis jedis = null;Boolean exists = null;try {jedis = getJedis();exists = jedis.hexists(key, field);}finally {if (jedis != null){jedis.close();}}return exists;}/*** 返回某个hash表中所有的key** @param key* @return*/public static Set<String> hkeys(String key) {Jedis jedis = null;Set<String> keys = null;try {jedis = getJedis();keys = jedis.hkeys(key);}finally {if (jedis != null){jedis.close();}}return keys;}/*** 返回某个hash表中所有的value** @param key* @return*/public static List<String> hvals(String key) {Jedis jedis = null;List<String> hvals = null;try {jedis = getJedis();hvals = jedis.hvals(key);}finally {if (jedis != null){jedis.close();}}return hvals;}/*** 删除某个hash表中的一个或多个键值对** @param key* @param fields*/public static void hdel(String key, String... fields) {Jedis jedis = null;try {jedis = getJedis();jedis.hdel(key, fields);}finally {if (jedis != null){jedis.close();}}}/*** 给某个hash表中的某个field的value增加多少** @param key hash表的key* @param field 表中的某个field* @param increment 增加多少* @return*/public static Long hincrby(String key, String field, Long increment) {Jedis jedis = null;Long result = null;try {jedis = getJedis();result = jedis.hincrBy(key, field, increment);}finally {if (jedis != null){jedis.close();}}return result;}/*######################## set(集合)的操作 ###########################*//*** 往set集合中添加一个或多个元素* @param key key* @param members 要添加的元素* @return 添加成功的元素个数*/public static Long sadd(String key,String... members){Jedis jedis = null;Long num = 0L;try {jedis = getJedis();num = jedis.sadd(key, members);}finally {if (jedis != null){jedis.close();}}return num;}/*** 返回set集合中的所有元素,顺序与加入时的顺序一致* @param key key* @return*/public static Set<String> smembers(String key){Jedis jedis = null;Set<String> members = null;try {jedis = getJedis();members = jedis.smembers(key);}finally {if (jedis != null){jedis.close();}}return members;}/*** 判断集合中是否存在某个元素* @param key key* @param member 某个元素* @return true存在,false不存在*/public static Boolean sismember(String key,String member){Jedis jedis = null;Boolean isMember = false;try {jedis = getJedis();isMember = jedis.sismember(key, member);}finally {if (jedis != null){jedis.close();}}return isMember;}/*** 返回set集合的长度* @param key key* @return*/public static Long scard(String key){Jedis jedis = null;Long len = 0L;try {jedis = getJedis();len = jedis.scard(key);}finally {if (jedis != null){jedis.close();}}return len;}/*** 删除set集合中指定的一个或多个元素* @param key* @param members 要删除的元素* @return 删除成功的元素个数*/public static Long srem(String key,String... members){Jedis jedis = null;Long num = 0L;try {jedis = getJedis();num = jedis.srem(key,members);}finally {if (jedis != null){jedis.close();}}return num;}/*** 将key1中的元素key1Member移动到key2中* @param key1 来源集合key* @param key2 目的地集合key* @param key1Member key1中的元素* @return 1成功,0失败*/public static Long smove(String key1,String key2,String key1Member){Jedis jedis = null;Long num = 0L;try {jedis = getJedis();num = jedis.smove(key1,key2,key1Member);}finally {if (jedis != null){jedis.close();}}return num;}/*** 随机查询返回集合中的指定个数的元素(若count为负数,返回的元素可能会重复)* @param key key* @param count 要查询返回的元素个数* @return 元素list集合*/public static List<String> srandmember(String key,int count){Jedis jedis = null;List<String> members = null;try {jedis = getJedis();members = jedis.srandmember(key,count);}finally {if (jedis != null){jedis.close();}}return members;}/*** 从set集合中随机弹出指定个数个元素* @param key key* @param count 要弹出的个数* @return 随机弹出的元素*/public static Set<String> spop(String key,int count){Jedis jedis = null;Set<String> members = null;try {jedis = getJedis();members = jedis.spop(key,count);}finally {if (jedis != null){jedis.close();}}return members;}/*** 求交集,返回多个set集合相交的部分* @param setKeys 多个set集合的key* @return 相交的元素集合*/public static Set<String> sinter(String... setKeys){Jedis jedis = null;Set<String> members = null;try {jedis = getJedis();members = jedis.sinter(setKeys);}finally {if (jedis != null){jedis.close();}}return members;}/*** 求并集,求几个set集合的并集(因为set中不会有重复的元素,合并后的集合也不会有重复的元素)* @param setKeys 多个set的key* @return 合并后的集合*/public static Set<String> sunion(String... setKeys){Jedis jedis = null;Set<String> members = null;try {jedis = getJedis();members = jedis.sunion(setKeys);}finally {if (jedis != null){jedis.close();}}return members;}/*** 求差集,求几个集合之间的差集* @param setKeys 多个set的key* @return 差集*/public static Set<String> sdiff(String... setKeys){Jedis jedis = null;Set<String> members = null;try {jedis = getJedis();members = jedis.sdiff(setKeys);}finally {if (jedis != null){jedis.close();}}return members;}/*######################## zset(有序集合)的操作 #######################*//*** 添加一个元素到zset* @param key key* @param score 元素的分数* @param member 元素* @return 成功添加的元素个数*/public static Long zadd(String key,double score, String member){Jedis jedis = null;Long num = null;try {jedis = getJedis();num = jedis.zadd(key,score,member);}finally {if (jedis != null){jedis.close();}}return num;}
}
简单举两个例子,可以仿照这个自己进行封装。
四、Jedis事务操作
#开启jedis事务
Transaction multi=jedis.multi();
#添加数据到redis中multi.set("ka", "va");
#jedis事务提交multi.exec();
#jedis事务回滚multi.discard();
Jedis jedis=new Jedis("127.0.0.1",6379);Transaction multi=jedis.multi();//开启redis事务try {//设置数据multi.set("ka", "va");multi.set("kb", "vb");int num=1/0;multi.set("kc", "vc");} catch (Exception e) {//multi.discard();e.printStackTrace();}//redis事务提交multi.exec();
以上代码的结果是:redis成功保存ka,kb,不会保存kc。Jedis jedis=new Jedis("127.0.0.1",6379);
//开启事务Transaction multi=jedis.multi();try {multi.set("ka", "va");multi.set("kb", "vb");int num=1/0; multi.set("kc", "vc");} catch (Exception e) {//redis事务回滚multi.discard();e.printStackTrace();}//redis事务回滚multi.exec(); 以上代码的结果是:redis不会保存ka,kb,kc.
五、Jedis 使用发布与订阅
整个过程可以分为以下几个步骤:
- 创建Redis连接;
- 创建订阅者;
- 创建发布者;
- 发布消息;
- 接收消息。
1.使用jedis的publish(String channel, String message)方法向通道发布消息;
2.使用jedis的subscribe(JedisPubSub jedisPubSub, String... channels)方法订阅通道;
3.jedisPubSub参数使用自定义类继承JedisPubSub类,重写onMessage(String channel, String message)方法即可处理接收的消息;
4.JedisPubSub类的方法可以按照需求重写:
public abstract class JedisPubSub {private int subscribedChannels = 0;private volatile Client client;public JedisPubSub() {}public void onMessage(String channel, String message) {}public void onPMessage(String pattern, String channel, String message) {}public void onSubscribe(String channel, int subscribedChannels) {}public void onUnsubscribe(String channel, int subscribedChannels) {}public void onPUnsubscribe(String pattern, int subscribedChannels) {}public void onPSubscribe(String pattern, int subscribedChannels) {}public void onPong(String pattern) {}
}
demo:
JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "192.168.100.155", 6379, 3000, "KFQ2xc3iozsa", 0);new Thread(new Runnable() {@Overridepublic void run() {while (true) {Jedis jedis = jedisPool.getResource();try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("发布消息");jedis.publish("ch", "hello");//发布消息jedis.close();}}}).start();MyPubSub mypubsub = new MyPubSub();new Thread(new Runnable() {@Overridepublic void run() {Jedis jedis = jedisPool.getResource();jedis.subscribe(mypubsub, "ch");//订阅消息jedis.close();}}).start();try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}mypubsub.unsubscribe();// 取消所有订阅}static class MyPubSub extends JedisPubSub {@Overridepublic void onSubscribe(String channel, int subscribedChannels) {System.out.println("订阅了:" + channel);super.onSubscribe(channel, subscribedChannels);}@Overridepublic void onMessage(String channel, String message) {System.out.println("收到通道" + channel + "消息:" + message);super.onMessage(channel, message);}@Overridepublic void onUnsubscribe(String channel, int subscribedChannels) {System.out.println("取消订阅:" + channel);super.onUnsubscribe(channel, subscribedChannels);}}
Redis总结(五)redis发布订阅模式 - 知乎 (zhihu.com)