Java注解詳解
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
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
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
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
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)容。