完美取代hashmap,SpringBoot整合Redis緩存DB數(shù)據(jù)

      網(wǎng)友投稿 984 2022-05-29

      目錄

      一、百度百科

      二、Redis下載

      三、Linux中安裝Redis

      1、上傳、解壓

      2、修改redis.conf配置文件,使其在后臺(tái)啟動(dòng)

      四、Java調(diào)用redis

      1、導(dǎo)入pom

      2、編寫Java主方法

      3、再次執(zhí)行主方法,執(zhí)行成功!

      五、五大數(shù)據(jù)類型代碼實(shí)例

      六、手機(jī)驗(yàn)證碼功能代碼實(shí)例

      七、Springboot整合Redis

      1、建工程,引入pom

      2、配置類

      3、控制類測(cè)試

      一、百度百科

      redis是一個(gè)key-value存儲(chǔ)系統(tǒng)。和Memcached類似,它支持存儲(chǔ)的value類型相對(duì)更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。這些數(shù)據(jù)類型都支持push/pop、add/remove及取交集并集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎(chǔ)上,redis支持各種不同方式的排序。與memcached一樣,為了保證效率,數(shù)據(jù)都是緩存在內(nèi)存中。區(qū)別的是redis會(huì)周期性的把更新的數(shù)據(jù)寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎(chǔ)上實(shí)現(xiàn)了master-slave(主從)同步。

      Redis 是一個(gè)高性能的key-value數(shù)據(jù)庫(kù)。 redis的出現(xiàn),很大程度補(bǔ)償了memcached這類key/value存儲(chǔ)的不足,在部 分場(chǎng)合可以對(duì)關(guān)系數(shù)據(jù)庫(kù)起到很好的補(bǔ)充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客戶端,使用很方便。

      Redis支持主從同步。數(shù)據(jù)可以從主服務(wù)器向任意數(shù)量的從服務(wù)器上同步,從服務(wù)器可以是關(guān)聯(lián)其他從服務(wù)器的主服務(wù)器。這使得Redis可執(zhí)行單層樹復(fù)制。存盤可以有意無意的對(duì)數(shù)據(jù)進(jìn)行寫操作。由于完全實(shí)現(xiàn)了發(fā)布/訂閱機(jī)制,使得從數(shù)據(jù)庫(kù)在任何地方同步樹時(shí),可訂閱一個(gè)頻道并接收主服務(wù)器完整的消息發(fā)布記錄。同步對(duì)讀取操作的可擴(kuò)展性和數(shù)據(jù)冗余很有幫助。

      二、Redis下載

      三、Linux中安裝Redis

      1、上傳、解壓

      Redis一般安裝在Linux環(huán)境下,開啟虛擬機(jī),通過xftp將redis壓縮包上傳到Linux服務(wù)器,并進(jìn)行解壓。

      2、修改redis.conf配置文件,使其在后臺(tái)啟動(dòng)

      四、Java調(diào)用redis

      1、導(dǎo)入pom

      redis.clients jedis 3.2.0

      1

      2

      3

      4

      5

      2、編寫Java主方法

      調(diào)用Redis中的ping方法,驚現(xiàn)異常:

      (1)開始的時(shí)候以為是防火墻的問題,后來通過查看redis狀態(tài)發(fā)現(xiàn)IP地址不對(duì),不應(yīng)該是127.0.0.1

      (2)修改redis.conf

      注意:需要注意的是在修改redis.conf時(shí),①注掉bind 127.0.0.1;②需要將本機(jī)訪問保護(hù)模式設(shè)置為no

      3、再次執(zhí)行主方法,執(zhí)行成功!

      五、五大數(shù)據(jù)類型代碼實(shí)例

      package com.guor.redis; import redis.clients.jedis.Jedis; import java.util.List; import java.util.Set; public class JedisTest01 { public static void main(String[] args) { test05(); } private static void test01(){ Jedis jedis = new Jedis("192.168.194.131", 6379); String value = jedis.ping(); System.out.println(value); //添加 jedis.set("name","GooReey"); //獲取 String name = jedis.get("name"); System.out.println(name); jedis.set("age","30"); jedis.set("city","dalian"); //獲取全部的key Set keys = jedis.keys("*"); for(String key : keys){ System.out.println(key+" --> "+jedis.get(key)); } //加入多個(gè)key和value jedis.mset("name1","zs","name2","ls","name3","ww"); List mget = jedis.mget("name1", "name2"); System.out.println(mget);//[zs, ls] } //list private static void test02(){ Jedis jedis = new Jedis("192.168.194.131", 6379); jedis.lpush("key1","01","02","03"); List values = jedis.lrange("key1",0,-1); System.out.println(values);//[03, 02, 01] } //set private static void test03(){ Jedis jedis = new Jedis("192.168.194.131", 6379); jedis.sadd("username","zs","ls","ww"); Set names = jedis.smembers("username"); System.out.println(names);//[ww, zs, ls] } //hash private static void test04(){ Jedis jedis = new Jedis("192.168.194.131", 6379); jedis.hset("users","age", "20"); String hget = jedis.hget("users","age"); System.out.println(hget); } //zset private static void test05(){ Jedis jedis = new Jedis("192.168.194.131", 6379); jedis.zadd("china",100d,"shanghai"); Set names = jedis.zrange("china",0,-1); System.out.println(names);//[shanghai] } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      六、手機(jī)驗(yàn)證碼功能代碼實(shí)例

      package com.guor.redis; import redis.clients.jedis.Jedis; import java.util.Random; public class PhoneCode { public static void main(String[] args) { verifyCode("10086");//795258 getRedisCode("10086","795258");//success. } //1、生成6位數(shù)字驗(yàn)證碼 public static String getCode(){ Random random = new Random(); String code = ""; for (int i = 0; i < 6; i++) { int rand = random.nextInt(10); code += rand; } return code;//849130 } //2、每個(gè)手機(jī)每天只能發(fā)送三次,驗(yàn)證碼放到redis中,設(shè)置過期時(shí)間 public static void verifyCode(String phone){ Jedis jedis = new Jedis("192.168.194.131", 6379); //拼接key //手機(jī)發(fā)送次數(shù)key String countKey = "VerifyCode" + phone + ":count"; //驗(yàn)證碼key String codeKey = "VerifyCode" + phone + ":code"; //每個(gè)手機(jī)每天只能發(fā)送三次 String count = jedis.get(countKey); if(count == null){ //設(shè)置過期時(shí)間 jedis.setex(countKey,24*60*60,"1"); }else if(Integer.parseInt(count)<=2){ //發(fā)送次數(shù)+1 jedis.incr(countKey); }else if(Integer.parseInt(count)>2){ System.out.println("今天的發(fā)送次數(shù)已經(jīng)超過三次"); jedis.close(); } String vCode = getCode(); jedis.setex(codeKey,120,vCode); jedis.close(); } //3、驗(yàn)證碼校驗(yàn) public static void getRedisCode(String phone, String code){ //從redis中獲取驗(yàn)證碼 Jedis jedis = new Jedis("192.168.194.131", 6379); //驗(yàn)證碼key String codeKey = "VerifyCode" + phone + ":code"; String redisCode = jedis.get(codeKey); if(redisCode.equals(code)){ System.out.println("success."); }else{ System.out.println("error"); } jedis.close(); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      完美取代hashmap,SpringBoot整合Redis緩存DB數(shù)據(jù)

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      當(dāng)超過三次時(shí):

      七、SpringBoot整合Redis

      1、建工程,引入pom

      4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASE com.guor redisspringboot 0.0.1-SNAPSHOT redisspringboot Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-data-redis 2.4.5 org.apache.commons commons-pool2 2.9.0 org.springframework.boot spring-boot-maven-plugin

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      2、配置類

      (1)application.properties

      # Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0) spring.redis.database=0 # Redis服務(wù)器地址 spring.redis.host=192.168.194.131 # Redis服務(wù)器連接端口 spring.redis.port=6379 # Redis服務(wù)器連接密碼(默認(rèn)為空) spring.redis.password= # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) spring.redis.jedis.pool.max-active=20 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制) spring.redis.jedis.pool.max-wait=-1 # 連接池中的最大空閑連接 spring.redis.jedis.pool.max-idle=10 # 連接池中的最小空閑連接 spring.redis.jedis.pool.min-idle=0 # 連接超時(shí)時(shí)間(毫秒) spring.redis.timeout=1000

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      (2)RedisConfig

      package com.guor.redisspringboot.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.*; import java.time.Duration; @EnableCaching @Configuration public class RedisConfig { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } /** * 基于SpringBoot2 對(duì) RedisCacheManager 的自定義配置 * @param redisConnectionFactory * @return */ @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { //初始化一個(gè)RedisCacheWriter RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); //設(shè)置CacheManager的值序列化方式為json序列化 RedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer(); RedisSerializationContext.SerializationPair pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer); RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair); //設(shè)置默認(rèn)超過時(shí)期是1天 defaultCacheConfig.entryTtl(Duration.ofDays(1)); //初始化RedisCacheManager return new RedisCacheManager(redisCacheWriter, defaultCacheConfig); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      3、控制類測(cè)試

      package com.guor.redisspringboot.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/redisTest") public class RedisTestController { @Autowired private RedisTemplate redisTemplate; @GetMapping public String getRedis(){ redisTemplate.opsForValue().set("name","zs"); String name = (String) redisTemplate.opsForValue().get("name"); return name; } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      Redis Spring Boot 數(shù)據(jù)庫(kù)

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:MySQL這樣學(xué)才叫了解!【4】
      下一篇:IC先生網(wǎng):什么是MEMS傳感器?類型、應(yīng)用特點(diǎn)介紹
      相關(guān)文章
      久久亚洲国产伦理| 在线亚洲精品自拍| 国产aⅴ无码专区亚洲av| 亚洲狠狠爱综合影院婷婷| 中文字幕精品三区无码亚洲| 亚洲一区中文字幕在线观看| 亚洲精品福利网泷泽萝拉| 亚洲AV色香蕉一区二区| 亚洲精品乱码久久久久久自慰| 成人午夜亚洲精品无码网站| 亚洲欧洲国产成人综合在线观看| 国产亚洲美女精品久久| 国产成人久久精品亚洲小说| 国产精品亚洲专区无码唯爱网| 亚洲av无码一区二区三区人妖| 亚洲AV一区二区三区四区| 亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片 | 亚洲一区中文字幕| 亚洲伊人久久大香线蕉啊| 精品亚洲AV无码一区二区| 亚洲宅男精品一区在线观看| 国产成人精品日本亚洲专| 国产色在线|亚洲| 亚洲精品av无码喷奶水糖心| 亚洲日韩AV一区二区三区中文| 亚洲av成人中文无码专区| 国产亚洲精品免费| 亚洲人成影院在线无码观看| 亚洲熟妇无码八AV在线播放| 久久亚洲精品AB无码播放| 亚洲国产另类久久久精品黑人| 亚洲av无码专区国产乱码在线观看 | 亚洲国产成人手机在线观看| 老牛精品亚洲成av人片| 亚洲AV网站在线观看| 三上悠亚亚洲一区高清| 亚洲αv久久久噜噜噜噜噜| 亚洲综合精品一二三区在线| 亚洲一卡2卡3卡4卡国产网站| 亚洲熟妇无码一区二区三区| 国产亚洲日韩在线a不卡|