當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot Redis
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot Redis
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Redis簡介
Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,并提供多種語言的API。
存儲類型
和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。這些數據類型都支持push/pop、add/remove及取交集并集和差集及更豐富的操作,而且這些操作都是原子性的。
數據追加方式
在此基礎上,redis支持各種不同方式的排序。與Memcached一樣,為了保證效率,數據都是緩存在內存中。區別的是redis會周期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎上實現了master-slave(主從)同步。
添加jar包依賴集成Redis
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency>RedisService常規操作
package com.jege.spring.boot.service;import java.io.Serializable; import java.util.Set; import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service;/*** 常規操作*/ @Service public class RedisService {@Autowiredprivate RedisTemplate redisTemplate;// 批量刪除對應的valuepublic void deleteAll(String... keys) {for (String key : keys) {delete(key);}}// 批量刪除keypublic void deletePattern(String pattern) {Set<Serializable> keys = redisTemplate.keys(pattern);if (keys.size() > 0)redisTemplate.delete(keys);}// 刪除指定key的valuepublic void delete(String key) {if (exists(key)) {redisTemplate.delete(key);}}// 判斷緩存中是否有對應的valuepublic boolean exists(String key) {return redisTemplate.hasKey(key);}// 讀取緩存public Object get(String key) {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();return operations.get(key);}// 寫入緩存public boolean set(String key, Object value) {boolean flag = false;try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();operations.set(key, value);flag = true;} catch (Exception e) {e.printStackTrace();}return flag;}// 寫入緩存public boolean set(String key, Object value, Long expireTime) {boolean flag = false;try {ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();operations.set(key, value);redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);flag = true;} catch (Exception e) {e.printStackTrace();}return flag;} }StringRedisService
package com.jege.spring.boot.service;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.BoundValueOperations; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service;/*** 直接操作String數據類型*/ @Service public class StringRedisService {@Autowiredpublic StringRedisTemplate stringRedisTemplate;// 獲取某個key的剩余過期時間public long residualExpirationTime(String key) {return stringRedisTemplate.getExpire(key);}// 當key不存在時,為key賦值public boolean setValue(String key, String value) {ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();return ops.setIfAbsent(key, value);}// 為key賦值,同時設置過期時間public void set(String key, String value, long time) {BoundValueOperations<String, String> ops = stringRedisTemplate.boundValueOps(key);ops.set(value, time, TimeUnit.SECONDS);}// 刪除某個keypublic void delete(String key) {stringRedisTemplate.delete(key);}// 判斷某個key是否存在public boolean exist(String key) {return stringRedisTemplate.hasKey(key);}// 同redis命令的leftpushpublic void leftPush(String key, String value) {stringRedisTemplate.boundListOps(key).leftPush(value);}// 同redis命令的rightpoppublic String rightPop(String key) {return stringRedisTemplate.boundListOps(key).rightPop();} }application.properties
spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.pool.max-idle=100 spring.redis.pool.min-idle=1 spring.redis.pool.max-active=1000 spring.redis.pool.max-wait=-1如果感覺不錯的話請記得點贊喲!!!
總結
以上是生活随笔為你收集整理的Spring Boot Redis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正则表达式语法详解
- 下一篇: Spring Boot swagger之