Redis是干什么的,Redis常用語(yǔ)法每日一練

      網(wǎng)友投稿 839 2025-04-03

      目錄

      一、百度百科

      二、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ù)制。存盤可以有意無(wú)意的對(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í)候以為是防火墻的問題,后來(lái)通過查看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

      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

      Redis是干什么的,Redis常用語(yǔ)法每日一練

      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 數(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)容。

      版權(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)容。

      上一篇:Excel如何插入批注和刪除批注
      下一篇:excel表格怎么求和(excel表格怎么求和一列)
      相關(guān)文章
      亚洲免费视频在线观看| 亚洲av午夜国产精品无码中文字| 亚洲国产中文在线二区三区免| 亚洲Av无码乱码在线播放| 久久精品国产亚洲AV电影 | 亚洲香蕉久久一区二区| 久久亚洲精品人成综合网| 亚洲熟女一区二区三区| 亚洲 自拍 另类小说综合图区 | 亚洲一区二区三区精品视频| 久久夜色精品国产噜噜亚洲AV| 亚洲熟女少妇一区二区| 亚洲国产精品自在拍在线播放| 亚洲国产精品成人AV在线| 亚洲日本国产综合高清| 亚洲精品成人久久| 亚洲精品视频在线观看免费 | 亚洲Av综合色区无码专区桃色| 久久影院亚洲一区| 狠狠亚洲婷婷综合色香五月排名| 最新亚洲成av人免费看| 亚洲精品蜜桃久久久久久| 亚洲成AV人片一区二区密柚| 亚洲AV无码1区2区久久| 中文字幕亚洲综合久久2| 亚洲免费观看在线视频| 亚洲中文无码卡通动漫野外| 亚洲精品无码高潮喷水A片软| 色窝窝亚洲av网| 亚洲午夜激情视频| 国产成人精品日本亚洲专区61 | 亚洲一区二区无码偷拍| 色天使亚洲综合一区二区| 亚洲精品国产福利一二区| 亚洲熟妇av一区二区三区漫画| 亚洲AV中文无码乱人伦下载| 亚洲尹人九九大色香蕉网站| 亚洲日本乱码一区二区在线二产线 | 爱情岛亚洲论坛在线观看| 久久久精品国产亚洲成人满18免费网站| 亚洲日韩一页精品发布|