火題大戰Vol.0 B 計數DP
阿新 • • 發佈:2020-08-19
SpringBoot整合
SpringBoot 操作資料:spring-data jpa jdbc mongodb redis! SpringData 也是和 SpringBoot 齊名的專案!
說明: 在 SpringBoot2.x 之後,原來使用的jedis被替換為了lettuce
jedis : 採用的直連,多個執行緒操作的話,是不安全的,如果想要避免不安全的,使用 jedis pool 連線 池! 更像 BIO 模式
lettuce : 採用netty,例項可以再多個執行緒中進行共享,不存線上程不安全的情況!可以減少執行緒資料 了,更像 NIO 模式
原始碼分析:
@Bean @ConditionalOnMissingBean(name = "redisTemplate") // 我們可以自己定義一個 redisTemplate來替換這個預設的! public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { // 預設的 RedisTemplate 沒有過多的設定,redis 物件都是需要序列化! // 兩個泛型都是 Object, Object 的型別,我們後使用需要強制轉換 <String, Object> RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean // 由於 String 是redis中最常使用的型別,所以說單獨提出來了一 個bean! public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
整合測試一下
-
匯入依賴
<!-- 操作redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
配置連線
# 配置redis spring.redis.host=127.0.0.1 spring.redis.port=6379
-
測試
package com.maple.redis.springBootRedis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.Set; /** * @author 楓葉 * @date 2020/8/1 18:56 * @Email [email protected] */ @SpringBootTest public class Test { @Autowired private RedisTemplate redisTemplate; @org.junit.jupiter.api.Test public void test() { // redisTemplate 操作不同的資料型別,api和我們的指令是一樣的 // opsForValue 操作字串 類似String // opsForList 操作List 類似List // opsForSet // opsForHash // opsForZSet // opsForGeo // opsForHyperLogLog // 除了進本的操作,我們常用的方法都可以直接通過redisTemplate操作,比如事務,和基本的CRUD // 獲取redis的連線物件 RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); connection.flushDb(); //設定key序列化器,預設是JdkSerializationRedisSerializer redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8); redisTemplate.opsForValue().set("myKey","楓葉正在測試redis"); redisTemplate.opsForValue().set("key2","楓葉正在測試redis2"); redisTemplate.opsForValue().set("key3","楓葉正在測試redis3"); System.out.println(redisTemplate.opsForValue().get("myKey")); Set keys = redisTemplate.keys("*"); System.out.println(keys.size()+"---"+keys.toString()); connection.close(); } }
執行結果
楓葉正在測試redis
3---[myKey, key3, key2]
我們來編寫一個自己的 RedisTemplete
package com.kuang.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { // 這是我給大家寫好的一個固定模板,大家在企業中,拿去就可以直接使用! // 自己定義了一個 RedisTemplate @Bean @SuppressWarnings("all") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { // 我們為了自己開發方便,一般直接使用 <String, Object> RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); // Json序列化配置 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // String 的序列化 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; } }
所有的redis操作,其實對於java開發人員來說,十分的簡單,更重要是要去理解redis的思想和每一種數 據結構的用處和作用場景!
Redis.conf詳解
啟動的時候,就通過配置檔案來啟動!
單位
配置檔案 unit單位 對大小寫不敏感!
包含
網路
bind 127.0.0.1 # 繫結的ip
protected-mode yes # 保護模式
port 6379 # 埠設定
通用 GENERAL
daemonize yes # 以守護程序的方式執行,預設是 no,我們需要自己開啟為yes!
pidfile /var/run/redis_6379.pid # 如果以後臺的方式執行,我們就需要指定一個 pid 檔案!
# 日誌
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably) 生產環境
# warning (only very important / critical messages are logged)
loglevel notice
logfile "" # 日誌的檔案位置名
databases 16 # 資料庫的數量,預設是 16 個數據庫
always-show-logo yes # 是否總是顯示LOGO
快照
持久化, 在規定的時間內,執行了多少次操作,則會持久化到檔案 .rdb. aof
redis 是記憶體資料庫,如果沒有持久化,那麼資料斷電及失!
# 如果900s內,如果至少有一個1 key進行了修改,我們就進行持久化操作
save 900 1
# 如果300s內,如果至少10 key進行了修改,我們就進行持久化操作
save 300 10
# 如果60s內,如果至少10000 key進行了修改,我們就進行持久化操作
save 60 10000
stop-writes-on-bgsave-error yes # 持久化如果出錯,是否還需要繼續工作!
rdbcompression yes # 是否壓縮 rdb 檔案,需要消耗一些cpu資源!
rdbchecksum yes # 儲存rdb檔案的時候,進行錯誤的檢查校驗!
dir ./ # rdb 檔案儲存的目錄!
REPLICATION 複製
SECURITY 安全
可以在這裡設定redis的密碼,預設是沒有密碼!
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> config get requirepass # 獲取redis的密碼
1) "requirepass"
2) ""
127.0.0.1:6379> config set requirepass "123456" # 設定redis的密碼
OK
127.0.0.1:6379> config get requirepass # 發現所有的命令都沒有許可權了
(error) NOAUTH Authentication required.
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456 # 使用密碼進行登入!
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"
限制 CLIENTS
maxclients 10000 # 設定能連線上redis的最大客戶端的數量
maxmemory <bytes> # redis 配置最大的記憶體容量
maxmemory-policy noeviction # 記憶體到達上限之後的處理策略
1、volatile-lru:只對設定了過期時間的key進行LRU(預設值)
2、allkeys-lru : 刪除lru演算法的key
3、volatile-random:隨機刪除即將過期key
4、allkeys-random:隨機刪除
5、volatile-ttl : 刪除即將過期的
6、noeviction : 永不過期,返回錯誤
APPEND ONLY 模式 aof配置
appendonly no # 預設是不開啟aof模式的,預設是使用rdb方式持久化的,在大部分所有的情況下,rdb完全夠用!
appendfilename "appendonly.aof" # 持久化的檔案的名字
# appendfsync always # 每次修改都會 sync。消耗效能
appendfsync everysec # 每秒執行一次 sync,可能會丟失這1s的資料!
# appendfsync no # 不執行 sync,這個時候作業系統自己同步資料,速度最快!
具體的配置,見持久化