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

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

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

      Spring Boot集成Redis

      1. Redis介紹

      Redis 是一種非關(guān)系型數(shù)據(jù)庫(NoSQL),并且是一個key-value存儲系統(tǒng)。和Memcached類似,它支持存儲的value類型相對更多,包括String(字符串)、List(鏈表)、Set(集合)、Z-Set(sorted set-有序集合)和Hash(哈希類型)。這些數(shù)據(jù)類型都支持push/pop、add/remove及取交集并集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎(chǔ)上,redis支持各種不同方式的排序。與memcached一樣,為了保證效率,數(shù)據(jù)都是緩存在內(nèi)存中。區(qū)別的是redis會周期性的把更新的數(shù)據(jù)寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎(chǔ)上實(shí)現(xiàn)了master-slave(主從)同步。它和傳統(tǒng)的關(guān)系型數(shù)據(jù)庫不一樣,不一定遵循傳統(tǒng)數(shù)據(jù)庫的一些基本要求,比如說SQL標(biāo)準(zhǔn),ACID屬性,表結(jié)構(gòu)等,這類數(shù)據(jù)庫主要有一下特點(diǎn):非關(guān)系型的、分布式的、開源的、水平可擴(kuò)展的。NoSQL 使用場景有:對數(shù)據(jù)高并發(fā)讀寫、對海量數(shù)據(jù)的高效率存儲和訪問、對數(shù)據(jù)的高可擴(kuò)展性和高可用性等。

      2. Redis安裝

      安裝使用的是在VMware虛擬機(jī)中Centos7鏡像中安裝的Redis,當(dāng)然也可以在阿里云服務(wù)器中進(jìn)行安裝Redis。只要使用命令行能ping的通云主機(jī)或者虛擬機(jī)的ip,然后在云主機(jī)或者虛擬機(jī)中放行對應(yīng)的端口號(或或者關(guān)掉防火墻)即可訪問Redis。以下是安裝步驟:

      • 安裝gcc編譯

      因?yàn)楹竺姘惭bredis的時候需要編譯,所以事先得先安裝gcc編譯。阿里云主機(jī)已經(jīng)默認(rèn)安裝了 gcc,如果是自己安裝的虛擬機(jī),那么需要先安裝一下 gcc:

      yum install gcc-c++

      • 創(chuàng)建一個存放Redis的文件夾,下載安裝包

      //創(chuàng)建一個文件夾用于存放Redis

      mkdir redisfile

      //進(jìn)入新創(chuàng)建的文件夾

      cd redisfile/

      //直接使用 wget 命令下載(這里使用的是4.0.9版本)

      wget http://download.redis.io/releases/redis-4.0.9.tar.gz

      • 解壓安裝包

      tar -zxvf redis-4.0.9.tar.gz

      • 開始安裝

      首先切換到redis-4.0.9文件夾下,然后進(jìn)行安裝

      //進(jìn)入redis-4.0.9文件夾

      cd redis-4.0.9

      //安裝

      make

      • 啟動Redis服務(wù)

      首先切換到src文件夾下,然后再啟動Redis服務(wù)

      //進(jìn)入src文件夾

      cd src

      //啟動服務(wù)

      ./redis-server

      3. Spring Boot集成Redis

      3.1 依賴導(dǎo)入

      Spring Boot 集成 redis 很方便,只需要導(dǎo)入一個 redis 的 starter 依賴即可。以來如下:

      <!-- 引入redis依賴 -->

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-data-redis</artifactId>

      </dependency>

      3.2 Redis配置

      導(dǎo)入依賴之后在application.properties文件中配置Redis,配置如下:

      ############################################################
      # REDIS 配置
      ############################################################
      spring.redis.database=0
      # 配置redis的主機(jī)地址,需要修改成自己的 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=
      # 如果賦值為-1,則表示不限制;如果pool已經(jīng)分配了maxActive個jedis實(shí)例,則此時pool的狀態(tài)為exhausted(耗盡) spring.redis.jedis.pool.max-active=8
      # 等待可用連接的最大時間,單位毫秒,默認(rèn)值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException spring.redis.jedis.pool.max-wait=-1
      # 連接池中的最大空閑連接,默認(rèn)值也是8。 spring.redis.jedis.pool.max-idle=10
      # 連接池中的最小空閑連接,默認(rèn)值也是0。 spring.redis.jedis.pool.min-idle=2 spring.redis.timeout=6000

      3.3 封裝Json工具類

      這個工具類比較簡單,封裝操作redisTemplate的實(shí)現(xiàn)類。這個工具類只是簡單的封裝了StringRedisTemplate,其他相關(guān)的數(shù)據(jù)類型大家可以根據(jù)自己的需要自行擴(kuò)展。內(nèi)容如下:

      package com.wyl.utils;
      
      import java.util.Map;
      import java.util.Set;
      import java.util.concurrent.TimeUnit;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.data.redis.core.StringRedisTemplate;
      import org.springframework.stereotype.Component;
      
      /**
       * 
       * @Title: RedisOperator.java
       * @Package com.wyl.utils
       * @Description: 使用redisTemplate的操作實(shí)現(xiàn)類
       * @author wyl
       * @date 2021年5月25日 下午4:11:22
       * @version V1.0
       */
      @Component
      public class RedisOperator {
         
      // @Autowired
      //    private RedisTemplate<String, Object> redisTemplate;
         
         @Autowired
         private StringRedisTemplate redisTemplate;
         
         // Key(鍵),簡單的key-value操作
      
         /**
          * 實(shí)現(xiàn)命令:TTL key,以秒為單位,返回給定 key的剩余生存時間(TTL, time to live)。
          * 
          * @param key
          * @return
          */
         public long ttl(String key) {
            return redisTemplate.getExpire(key);
         }
         
         /**
          * 實(shí)現(xiàn)命令:expire 設(shè)置過期時間,單位秒
          * 
          * @param key
          * @return
          */
         public void expire(String key, long timeout) {
            redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
         }
         
         /**
          * 實(shí)現(xiàn)命令:INCR key,增加key一次
          * 
          * @param key
          * @return
          */
         public long incr(String key, long delta) {
            return redisTemplate.opsForValue().increment(key, delta);
         }
      
         /**
          * 實(shí)現(xiàn)命令:KEYS pattern,查找所有符合給定模式 pattern的 key
          */
         public Set<String> keys(String pattern) {
            return redisTemplate.keys(pattern);
         }
      
         /**
          * 實(shí)現(xiàn)命令:DEL key,刪除一個key
          * 
          * @param key
          */
         public void del(String key) {
            redisTemplate.delete(key);
         }
      
         // String(字符串)
      
         /**
          * 實(shí)現(xiàn)命令:SET key value,設(shè)置一個key-value(將字符串值 value關(guān)聯(lián)到 key)
          * 
          * @param key
          * @param value
          */
         public void set(String key, String value) {
            redisTemplate.opsForValue().set(key, value);
         }
      
         /**
          * 實(shí)現(xiàn)命令:SET key value EX seconds,設(shè)置key-value和超時時間(秒)
          * 
          * @param key
          * @param value
          * @param timeout
          *            (以秒為單位)
          */
         public void set(String key, String value, long timeout) {
            redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
         }
      
         /**
          * 實(shí)現(xiàn)命令:GET key,返回 key所關(guān)聯(lián)的字符串值。
          * 
          * @param key
          * @return value
          */
         public String get(String key) {
            return (String)redisTemplate.opsForValue().get(key);
         }
      
         // Hash(哈希表)
      
         /**
          * 實(shí)現(xiàn)命令:HSET key field value,將哈希表 key中的域 field的值設(shè)為 value
          * 
          * @param key
          * @param field
          * @param value
          */
         public void hset(String key, String field, Object value) {
            redisTemplate.opsForHash().put(key, field, value);
         }
      
         /**
          * 實(shí)現(xiàn)命令:HGET key field,返回哈希表 key中給定域 field的值
          * 
          * @param key
          * @param field
          * @return
          */
         public String hget(String key, String field) {
            return (String) redisTemplate.opsForHash().get(key, field);
         }
      
         /**
          * 實(shí)現(xiàn)命令:HDEL key field [field ...],刪除哈希表 key 中的一個或多個指定域,不存在的域?qū)⒈缓雎浴?    * 
          * @param key
          * @param fields
          */
         public void hdel(String key, Object... fields) {
            redisTemplate.opsForHash().delete(key, fields);
         }
      
         /**
          * 實(shí)現(xiàn)命令:HGETALL key,返回哈希表 key中,所有的域和值。
          * 
          * @param key
          * @return
          */
         public Map<Object, Object> hgetall(String key) {
            return redisTemplate.opsForHash().entries(key);
         }
      
         // List(列表)
      
         /**
          * 實(shí)現(xiàn)命令:LPUSH key value,將一個值 value插入到列表 key的表頭
          * 
          * @param key
          * @param value
          * @return 執(zhí)行 LPUSH命令后,列表的長度。
          */
         public long lpush(String key, String value) {
            return redisTemplate.opsForList().leftPush(key, value);
         }
      
         /**
          * 實(shí)現(xiàn)命令:LPOP key,移除并返回列表 key的頭元素。
          * 
          * @param key
          * @return 列表key的頭元素。
          */
         public String lpop(String key) {
            return (String)redisTemplate.opsForList().leftPop(key);
         }
      
         /**
          * 實(shí)現(xiàn)命令:RPUSH key value,將一個值 value插入到列表 key的表尾(最右邊)。
          * 
          * @param key
          * @param value
          * @return 執(zhí)行 LPUSH命令后,列表的長度。
          */
         public long rpush(String key, String value) {
            return redisTemplate.opsForList().rightPush(key, value);
         }
      }

      3.4 創(chuàng)建RedisController控制器

      package com.wyl.controller;
      
      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.data.redis.core.StringRedisTemplate;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import com.wyl.utils.JSONResult;
      import com.wyl.pojo.SysUser;
      import com.wyl.pojo.User;
      import com.wyl.utils.JsonUtils;
      import com.wyl.utils.RedisOperator;
      
      @RestController
      @RequestMapping("redis")
      public class RedisController {
          
          @Autowired
          private StringRedisTemplate strRedis;
          
          @Autowired
          private RedisOperator redis;
          
          @RequestMapping("/test")
          public JSONResult test() {
              SysUser user = new SysUser();
              user.setId("20210001");
              user.setUsername("admin");
              user.setPassword("123456");
              user.setIsDelete(0);
              user.setRegistTime(new Date());
              strRedis.opsForValue().set("json:user", JsonUtils.objectToJson(user));
      
              return JSONResult.ok(user);
          }
          
          @RequestMapping("/getJsonList")
          public JSONResult getJsonList() {
              
              User user = new User();
              user.setAge(18);
              user.setName("張三");
              user.setPassword("123456");
              user.setBirthday(new Date());
              
              User u1 = new User();
              u1.setAge(19);
              u1.setName("李四");
              u1.setPassword("123456");
              u1.setBirthday(new Date());
              
              User u2 = new User();
              u2.setAge(17);
              u2.setName("王五");
              u2.setPassword("123456");
              u2.setBirthday(new Date());
              
              List<User> userList = new ArrayList<>();
              userList.add(user);
              userList.add(u1);
              userList.add(u2);
              
              redis.set("json:info:userlist", JsonUtils.objectToJson(userList), 2000);
              
              String userListJson = redis.get("json:info:userlist");
              List<User> userListBorn = JsonUtils.jsonToList(userListJson, User.class);
              
              return JSONResult.ok(userListBorn);
          }
      }
      • 說明

          1. test: 是沒有封裝的,原生的Redis 客戶端操作Redis的方法。

          2. getJsonList :是封裝的工具類操作調(diào)用方法。

      3.5 測試

      在瀏覽器中輸入:http://localhost:8080/redis/test 查看是否有數(shù)據(jù)返回即可。

      4. 總結(jié)

      以上內(nèi)容主要介紹了Redis的使用場景、安裝過程,以及Spring Boot 中集成Redis 的詳細(xì)步驟。在實(shí)際項(xiàng)目中,通常使用Redis作為緩存,在查詢數(shù)據(jù)庫時,會先從Redis中查找,如果有信息就從Redis中取;如果沒有則從數(shù)據(jù)庫中查,并且同步到Redis中,下次Redis中就有了。更新和刪除也是需要同步到Redis中。Redis在高并發(fā)場景下運(yùn)用的非常廣泛。

      posted @ 2021-05-28 11:22  文藝龍  閱讀(487)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲 卡通 欧美 制服 中文 | 国产成人免费午夜在线观看| 久热这里有精品免费视频| 草草线在成年免费视频2| 米奇影院888奇米色99在线| 国产精品久久久久不卡绿巨人| 97人妻成人免费视频| 内射人妻视频国内| 国产九九视频一区二区三区| 最新亚洲人成无码网站欣赏网| 亚洲av日韩在线资源| 亚洲国产中文字幕在线视频综合 | 人妻在线中文字幕| 亚洲一二三区精品与老人| 免费国产va在线观看| 亚洲av中文乱码一区二| 国产一区二区三区免费观看| 亚洲欧美人成人让影院| 国产激情一区二区三区不卡| 激情综合色综合啪啪开心| 狠狠色噜噜狠狠狠狠777米奇| 国产精品人妻中文字幕| 色8久久人人97超碰香蕉987| av一区二区中文字幕| 国产精品自拍中文字幕| 平泉县| 夜色福利站WWW国产在线视频| 久久精品人人槡人妻人人玩AV | 亚洲中文字幕人妻系列| 人人爽亚洲aⅴ人人爽av人人片 | 少妇被粗大的猛进69视频| 无码精品人妻一区二区三区中 | 粉嫩av国产一区二区三区| 大香伊蕉在人线国产最新2005| gogo无码大胆啪啪艺术| 国产精品∧v在线观看| 亚洲一区二区三级av| 久章草这里只有精品| 国精品无码人妻一区二区三区| 中文字幕国产精品第一页| 久青草国产在视频在线观看|