<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      RedisTemplate操作命令 - 環(huán)境

      不斷記錄中...


       

      SpringBoot整合redis

       

      pom依賴:

              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-redis</artifactId>
                  <version>2.1.6.RELEASE</version>
              </dependency>
      
              <dependency>
                  <groupId>org.redisson</groupId>
                  <artifactId>redisson</artifactId>
                  <version>3.11.2</version>
              </dependency>    
      View Code
      這里用redisson,因為項目中用了redisson做分布式鎖。

      配置(application-dev.yml):
      spring:
        application:
          name: product-web
        redis:
          #數(shù)據(jù)庫索引
          database: 0
          host: 127.0.0.1
          port: 6379
          password: ******
          jedis:
            pool:
              #最大連接數(shù)
              max-active: 500
              #最大空閑
              max-idle: 1000
              #最小空閑
              min-idle: 4
              max-wait: 6000ms
      

       

      例 說明:

      @Autowired
      RedisTemplate redisTemplate;

      @Autowired
      StringRedisTemplate stringRedisTemplate;
      redisTemplate.opsForValue().set(key, value);
      redisTemplate.delete(key);
      
      public void set(byte[] key, byte[] value) {
         if (key != null) {
            this.redisTemplate.opsForValue().set(key, value);
         }
      }
      
      

        

      redis文件夾形式:

      以:做key的分割線可設(shè)置歸類(文件夾形式)

      @Test
          public void distoryTest(){
              String root = "root";
              String path = "dir";
              String key = "dirTestKey";
              redisTemplate.opsForValue().set(root+":"+path+":"+key, "1");
          }

      顯示形式:

       

       

      RedisClient,引用并封裝了上面兩個redisTemplate,可以直接執(zhí)行g(shù)et、set類似操作

      import com.qulv.vdn.common.constant.ConfigConstant;
      import lombok.extern.slf4j.Slf4j;
      import org.apache.commons.lang3.StringUtils;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.data.redis.core.*;
      import org.springframework.stereotype.Component;
      
      import javax.annotation.Resource;
      import java.io.IOException;
      import java.util.*;
      import java.util.concurrent.TimeUnit;
      
      @SuppressWarnings("ALL")
      @Slf4j
      @Component("redisClient")
      public class RedisClient {
         public static final long DEFAULT_EXPIRE = 86400L;
         public static final long NOT_EXPIRE = -1L;
      
         @Autowired
         RedisTemplate redisTemplate;
      
         @Autowired
         StringRedisTemplate stringRedisTemplate;
      
         @Resource(name = "stringRedisTemplate")
         private ValueOperations<String, String> valueOperations;
      
         @Resource(name = "stringRedisTemplate")
         private HashOperations<String, String, String> hashOperations;
      
         public void del(String key) {
            if (this.exists(key)) {
               this.stringRedisTemplate.delete(key);
            }
         }
      
         public void set(String key, String value, long liveTime) {
            if (!StringUtils.isBlank(key)) {
               this.valueOperations.set(key, value);
               if (liveTime != NOT_EXPIRE) {
                  this.stringRedisTemplate.expire(key, liveTime, TimeUnit.SECONDS);
               }
            }
         }
      
         public void set(String key, String value) {
            this.set(key, value, NOT_EXPIRE);
         }
      
         public void set(byte[] key, byte[] value) {
            if (key != null) {
               this.redisTemplate.opsForValue().set(key, value);
            }
         }
      
         public Set<String> scan(String pattern) throws Exception{
            return ConfigConstant.OPEN_REDIS_CLUSTER_NO.equals("false") ? (Set)this.stringRedisTemplate.execute((RedisCallback<Set>) connection -> {
               Cursor<byte[]> cursor = null;
               Set<String> set = new HashSet<>();
      
               try {
                  cursor = connection.scan(ScanOptions.scanOptions().count(2147483647L).match(pattern).build());
      
                  while(cursor.hasNext()) {
                     String element = new String((byte[])cursor.next());
                     set.add(element);
                  }
                  Set<String> var16 = set;
                  return set;
               } catch (Exception var14) {
                  log.error("【[redisClient]使用scan出現(xiàn)異常,異常原因={}】", var14.getMessage());
               } finally {
                  if (cursor != null) {
                     try {
                        cursor.close();
                     } catch (IOException var13) {
                        log.error("【[redisClient]關(guān)閉游標異常,異常原因={}】", var13.getMessage());
                     }
                  }
               }
               return null;
            }) : this.keysCluster(pattern);
         }
      
         public String get(String key) {
            if (StringUtils.isBlank(key)) {
               return null;
            } else {
               return this.stringRedisTemplate.opsForValue().get(key) == null ? null : this.stringRedisTemplate.opsForValue().get(key).toString();
            }
         }
      
         public Set<String> keysCluster(String pattern) {
            Set<String> result = new HashSet();
            Set<String> nodesSet = this.getClusterNodes();
            return (Set)(nodesSet == null && nodesSet.size() == 0 ? result : (Set)this.stringRedisTemplate.execute((RedisCallback<Set>)connection -> {
               Iterator iterator = nodesSet.iterator();
      
               while(iterator.hasNext()) {
                  byte[] args1 = pattern.getBytes();
                  byte[] args2 = ((String)iterator.next()).getBytes();
                  ArrayList object = (ArrayList)connection.execute("keys", new byte[][]{args1, args2});
                  Iterator it = object.iterator();
      
                  while(it.hasNext()) {
                     String keyName = new String((byte[])it.next());
                     result.add(keyName);
                  }
               }
      
               return result;
            }));
         }
      
         public Set<String> getClusterNodes() {
            return (Set)this.stringRedisTemplate.execute((RedisCallback<Set>)connection -> {
               Set<String> nodes = new HashSet();
               byte[] args1 = "nodes".getBytes();
               byte[] object = (byte[])((byte[])connection.execute("cluster", new byte[][]{args1}));
               if (object == null) {
                  return null;
               } else {
                  String string = new String(object);
                  if (StringUtils.isEmpty(string)) {
                     return null;
                  } else {
                     String[] nodeInfoArray = string.split("\n");
      
                     for(int i = 0; i < nodeInfoArray.length; ++i) {
                        if (nodeInfoArray[i].contains("master")) {
                           String[] nodeInfo = nodeInfoArray[i].split(" ");
                           String nodeName = nodeInfo[0];
                           nodes.add(nodeName);
                        }
                     }
      
                     return nodes;
                  }
               }
            });
         }
      
         public void updateCmdStatus(String uin, String userName, String cmdId, String status) {
            if (cmdId != null) {
               this.set(uin + ":cmdId:" + userName + ":" + cmdId, status, DEFAULT_EXPIRE);
            }
      
         }
      
         public String getCmdIdStatus(String uin, String userName, String cmdId) {
            return this.get(uin + ":cmdId:" + userName + ":" + cmdId);
         }
      
         public void expire(String key, long liveTime) {
            stringRedisTemplate.expire(key, liveTime, TimeUnit.SECONDS);
         }
      
         public Set<String> keys(String pattern) {
            return this.stringRedisTemplate.keys(pattern);
         }
      
         public boolean exists(String key) {
            return this.stringRedisTemplate.hasKey(key);
         }
      
         public void lpush(String key, String value) {
            this.stringRedisTemplate.opsForList().leftPush(key, value);
         }
      
         public void rpush(String key, String value) {
            this.stringRedisTemplate.opsForList().rightPush(key, value);
         }
      
         public Long llen(String key) {
            return this.stringRedisTemplate.opsForList().size(key);
         }
      
         public String lindex(String key, Long index) {
            return this.stringRedisTemplate.opsForList().index(key, index) == null ? null : this.stringRedisTemplate.opsForList().index(key, index).toString();
         }
      
         public String lpop(String key) {
            try {
               return this.stringRedisTemplate.opsForList().leftPop(key).toString();
            } catch (NullPointerException n) {
               //throw new NullPointerException(" opsForList is null ");
               return null;
            }
         }
      
         public List<String> lrange(String key, long start, long end) {
            return this.stringRedisTemplate.opsForList().range(key, start, end);
         }
      
         public void hset(String key, String field, String value) {
            this.hset(key, field, value, NOT_EXPIRE);
         }
      
         public void hset(String key, String field, String value, long liveTime) {
            if (!StringUtils.isBlank(key)) {
               hashOperations.put(key, field, value);
               if (liveTime != NOT_EXPIRE) {
                  this.stringRedisTemplate.expire(key, liveTime, TimeUnit.SECONDS);
               }
            }
         }
      
         public void hsetAll(String key, Map<String, String> m, long liveTime) {
            if (!StringUtils.isBlank(key)) {
               hashOperations.putAll(key, m);
               if (liveTime != NOT_EXPIRE) {
                  this.stringRedisTemplate.expire(key, liveTime, TimeUnit.SECONDS);
               }
            }
         }
      
         public void hsetAll(String key, Map<String, String> m) {
            this.hsetAll(key, m, NOT_EXPIRE);
         }
      
         public String hget(String key, String field) {
            return this.stringRedisTemplate.opsForHash().get(key, field) == null ? null : this.stringRedisTemplate.opsForHash().get(key, field).toString();
         }
      
         public void hdel(String key, String field) {
            this.stringRedisTemplate.opsForHash().delete(key, new Object[]{field});
         }
      
         public Map<String, String> hgetall(String key) {
            return this.hashOperations.entries(key);
         }
      
         public boolean hasKey(String key, String field) {
            return this.stringRedisTemplate.opsForHash().hasKey(key, field);
         }
      
         public void hasInc(String key, String hashKey, long delta){
            stringRedisTemplate.opsForHash().increment(key, hashKey, delta);
         }
      
         public Set zrang(String key, Long start, Long stop) {
            return this.stringRedisTemplate.opsForZSet().range(key, start, stop);
         }
      
         public Set zrangeByScore(String key, double v1, double l1) {
            return this.stringRedisTemplate.opsForZSet().rangeByScore(key, v1, l1);
         }
      
         public void zremrangeByScore(String key, double v1, double l1) {
            this.stringRedisTemplate.opsForZSet().removeRangeByScore(key, v1, l1);
         }
      
         public void zadd(String key, String value, Long score) {
            this.stringRedisTemplate.opsForZSet().add(key, value, (double)score);
         }
      
         public void zaddlive(String key, String value, Long score, long liveTime) {
            this.stringRedisTemplate.opsForZSet().add(key, value, (double)score);
            this.stringRedisTemplate.expire(key, liveTime, TimeUnit.SECONDS);
         }
      
         public void zremoveRange(String key, Long start, Long stop) {
            this.stringRedisTemplate.opsForZSet().removeRange(key, start, stop);
         }
      
         public void saveMessage(String uin, String userName, String msgId, String message) {
            this.hset(uin + ":message:" + userName, msgId, message);
         }
      
         public Set<String> getRedisMessageRecord(String uin, String wechatUserName, String wechatContactUserName) {
            Set messageSet = null;
      
            try {
               messageSet = this.stringRedisTemplate.opsForZSet().reverseRange(uin + ":messageRecord:" + wechatUserName + ":" + wechatContactUserName, 0L, 30L);
            } catch (Exception var6) {
               var6.printStackTrace();
            }
      
            return messageSet;
         }
      }
      View Code

       

      使用示例:

          /**
           * 設(shè)置邀請碼
           * @param user
           */
          public String setInvitationCode(User user){
              if (user.getUserId() == 1){
                  return "";
              } else {
                  String code = UUIDUtil.getInvitationCode(user.getUserId()) ;
                  Set<String> keys = redisClient.keys("*#"+user.getUserId());
                  if (keys.isEmpty()){
                      //設(shè)置代理人相關(guān)信息
                      redisClient.set(code + "_vest_in", getAgentVestIn(user), 60 * 24 * 15);
                      redisClient.set(code, String.valueOf(user.getUserId()), 60 * 24 * 15);
                      user.setInviteCode(code);
                  }else {
                      code = keys.stream().findFirst().get();
                  }
                  return code;
              }
          }

       

       

       

       




      posted on 2019-12-13 11:10  GhostSugar  閱讀(2963)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 久女女热精品视频在线观看| 亚洲人亚洲人成电影网站色 | 日本亚洲一级中文字幕| 无码人妻精品一区二区在线视频| 亚洲の无码国产の无码步美| 精品视频在线观看免费观看| 又湿又紧又大又爽A视频男| 亚洲男人AV天堂午夜在| 伊人av超碰伊人久久久| 欧美 日韩 国产 成人 在线观看| 久久综合伊人| 亚洲色欲色欱WWW在线| 龙江县| 亚洲伊人久久综合成人| 亚洲国产永久精品成人麻豆| 欧美深度肠交惨叫| 综合久久av一区二区三区| 亚洲欧洲日韩精品在线| 国产精品福利自产拍久久| gogogo高清在线播放免费| 国99久9在线 | 免费| 先锋影音男人av资源| 午夜欧美精品久久久久久久| 精品国产一区二区三区av性色| 中文字幕无码乱码人妻系列蜜桃| 成人小说亚洲一区二区三区| 97精品亚成在人线免视频| 日韩精品人妻av一区二区三区| 色综合夜夜嗨亚洲一二区| 国产精品久久久午夜夜伦鲁鲁| 精品不卡一区二区三区| 久久三级国内外久久三级| 精品人妻少妇一区二区三区在线| 久久99久国产精品66| 亚洲国产精品第一二三区| 国产精品天天看天天狠| 农民人伦一区二区三区| 国产成人午夜精品福利| 国产成人精彩在线视频| 亚洲色欲色欲WWW在线丝| 国产午夜福利av在线麻豆|