package com.bootdo.ceshiceshi.redisceshi; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Tuple; import java.io.IOException; import java.util.*; public class JedisClusterUtil { private JedisClusterUtil() { } static String prefix = "luffi:lbl"; static String KEY_SPLIT = ":"; //用于隔开缓存前缀与缓存键值 public static void main(String[] args) { JedisCluster jedis = null; try { jedis = getJedis(); operSortedSet(jedis); //todo 操作set /* operSet(jedis);*/ //todo 操作list /* operList(jedis);*/ //todo 操作hash /* operHash(jedis);*/ //todo 操作String /* operStr(jedis);*/ } catch (Exception e) { e.printStackTrace(); returnJedis(jedis); } finally { returnJedis(jedis); } } /** * 操作sorted set-自动排序 */ public static void operSortedSet(JedisCluster jedis ) { Map<String, Double> scoreMembers = new HashMap<String, Double>(); scoreMembers.put("小明", 89D); scoreMembers.put("xiaopeng", 93D); scoreMembers.put("小胡", 88D); // 添加 jedis.zadd("score", scoreMembers); // 获取start :起始条数 end:结束条数 按分数升序查询 Set<String> strSet = jedis.zrange("score", 0, 1); for (String string : strSet) { System.out.println(string); } System.out.println("-------------"); // 降序查询,并获取成员的分数 Set<Tuple> tupleSet = jedis.zrevrangeWithScores("score", 0, 1); for (Tuple tuple : tupleSet) { // 成员 String element = tuple.getElement(); Double score = tuple.getScore(); System.out.println(element + ":" + score); } System.out.println("-----------------"); // 获取总条数 Long total = jedis.zcard("score"); System.out.println("总条数:" + total); // 删除 jedis.zrem("score", "xiaopeng"); } /** * 操作set */ public static void operSet(JedisCluster jedis) { String keyA = "{" + prefix + KEY_SPLIT + "set}a"; String keyB = "{" + prefix + KEY_SPLIT + "set}b"; jedis.del(keyA); jedis.del(keyB); System.out.println(jedis.sadd(keyA, "a", "b", "c"));//给集合添加数据 System.out.println(jedis.sadd(keyA, "a"));//给集合添加数据.集合是不可以重复的 System.out.println(jedis.sadd(keyA, "d"));//给集合添加数据 System.out.println(jedis.smembers(keyA));//返回集合所有数据 } /** * 操作list */ public static void operList(JedisCluster jedis) { // 添加 for (int i = 0; i < 10; i++) { jedis.lpush("animals3333", "dog" + i, " " + i, "fish" + i); } // 获取 String reString = jedis.rpop("animals3333"); System.out.println(reString); // 分页查询 start:起始条数 end :结束条数 List<String> strList = jedis.lrange("animals3333", 0, 9); for (String string : strList) { System.out.println(string); } System.out.println("----------------"); // 获取总条数 Long total = jedis.llen("animals3333"); System.out.println("总条数" + total); // 删除 jedis.lrem("animals3333", 1, "dog0"); jedis.del("animals3333"); } /** * 操作hash */ public static void operHash(JedisCluster jedis) { // 添加一条 jedis.hset("goodsInfo", "goodsName", "403-超级手机"); // 获取一条 String goodsName = jedis.hget("goodsInfo", "goodsName"); System.out.println("商品名称" + goodsName); Map<String, String> hash = new HashMap<String, String>(); hash.put("orderSn", "20171226122301"); hash.put("orderStatus", "提交预订单"); // 添加多条 jedis.hmset("orderInfo", hash); System.out.println("---------------"); // 获取多条 List<String> strList = jedis.hmget("orderInfo", "orderSn", "orderStatus"); for (String string : strList) { System.out.println(string); } System.out.println("---------------"); // 获取全部 Map<String, String> orderInfoMap = jedis.hgetAll("orderInfo"); for (Map.Entry<String, String> entry : orderInfoMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } // 删除 jedis.hdel("orderInfo", "orderStatus"); } /** * 操作String */ public static void operStr(JedisCluster jedis) { // 添加一条数据 jedis.set("username", "jonychen"); System.out.println("用户名------" + jedis.get("username")); jedis.append("username", " is my lover meng"); //拼接 System.out.println("用户名------" + jedis.get("username")); // 删除 jedis.del("username"); } /** * 提供了Jedis的对象 * * @return */ public static JedisCluster getJedis() { final String redisURL = "服务器地址"; final String auth = ""; final int port7001 = 7000; final int port7002 = 7001; final int port7003 = 7002; final int port7004 = 7003; final int port7005 = 7004; final int port7006 = 7005; final int MAX_IDLE = 200; final int MAX_TOTAL = 1024; final int CONN_TIME_OUT = 1000;//链接server超时 final int SO_TIME_OUT = 1000;//等待response超时 final int MAX_ATTEMPTS = 1;//尝试重新链接次数 GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(MAX_TOTAL); poolConfig.setMaxIdle(MAX_IDLE); HostAndPort hostAndPort1 = new HostAndPort(redisURL, port7001); HostAndPort hostAndPort2 = new HostAndPort(redisURL, port7002); HostAndPort hostAndPort3 = new HostAndPort(redisURL, port7003); HostAndPort hostAndPort4 = new HostAndPort(redisURL, port7004); HostAndPort hostAndPort5 = new HostAndPort(redisURL, port7005); HostAndPort hostAndPort6 = new HostAndPort(redisURL, port7006); Set<HostAndPort> hostAndPortSet = new HashSet<>(6); hostAndPortSet.add(hostAndPort1); hostAndPortSet.add(hostAndPort2); hostAndPortSet.add(hostAndPort3); hostAndPortSet.add(hostAndPort4); hostAndPortSet.add(hostAndPort5); hostAndPortSet.add(hostAndPort6); // public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, // int soTimeout, int maxAttempts, String password, GenericObjectPoolConfig poolConfig) JedisCluster jedisCluster1 = new JedisCluster(hostAndPortSet, CONN_TIME_OUT, SO_TIME_OUT, MAX_ATTEMPTS, poolConfig); return jedisCluster1; } /** * 资源释放 * * @param jedis */ public static void returnJedis(JedisCluster jedis) { try { if (jedis != null) { jedis.close(); } } catch (IOException e) { e.printStackTrace(); } } }
JedisCluster操作redis集群demo
最新推荐文章于 2023-08-31 09:05:46 发布