国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Spring Boot 菜鳥教程 22 Redis

2019-11-06 06:25:57
字體:
來源:轉載
供稿:網友

GitHub

簡介

Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,并提供多種語言的API。

存儲類型

和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set –有序集合)和hash(哈希類型)。這些數據類型都支持push/pop、add/remove及取交集并集和差集及更豐富的操作,而且這些操作都是原子性的。

數據追加方式

在此基礎上,redis支持各種不同方式的排序。與Memcached一樣,為了保證效率,數據都是緩存在內存中。區(qū)別的是redis會周期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,并且在此基礎上實現(xiàn)了master-slave(主從)同步。

添加jar包依賴集成Redis

<dependency> <groupId>org.sPRingframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>

RedisService常規(guī)操作

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;/** * @author JE哥 * @email 1272434821@QQ.com * @description:常規(guī)操作 */@Servicepublic class RedisService { @Autowired private RedisTemplate redisTemplate; // 批量刪除對應的value public void deleteAll(String... keys) { for (String key : keys) { delete(key); } } // 批量刪除key public void deletePattern(String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } // 刪除指定key的value public void delete(String key) { if (exists(key)) { redisTemplate.delete(key); } } // 判斷緩存中是否有對應的value public 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;/** * @author JE哥 * @email 1272434821@qq.com * @description:直接操作String數據類型 */@Servicepublic class StringRedisService { @Autowired public 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); } // 刪除某個key public void delete(String key) { stringRedisTemplate.delete(key); } // 判斷某個key是否存在 public boolean exist(String key) { return stringRedisTemplate.hasKey(key); } // 同redis命令的leftpush public void leftPush(String key, String value) { stringRedisTemplate.boundListOps(key).leftPush(value); } // 同redis命令的rightpop public String rightPop(String key) { return stringRedisTemplate.boundListOps(key).rightPop(); }}

application.properties

spring.redis.host=localhostspring.redis.port=6379spring.redis.passWord=spring.redis.pool.max-idle=100 spring.redis.pool.min-idle=1spring.redis.pool.max-active=1000spring.redis.pool.max-wait=-1

其他關聯(lián)項目

Spring Boot 菜鳥教程 14 動態(tài)修改定時任務cron參數 http://blog.csdn.net/je_ge/article/details/53434321

源碼地址

https://github.com/je-ge/spring-boot

如果覺得我的文章或者代碼對您有幫助,可以請我喝杯咖啡。 您的支持將鼓勵我繼續(xù)創(chuàng)作!謝謝! 微信打賞 支付寶打賞


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长武县| 焉耆| 福海县| 安岳县| 阜阳市| 原阳县| 班玛县| 收藏| 云梦县| 喀喇沁旗| 华蓥市| 泾阳县| 安岳县| 宁南县| 阳东县| 彰化县| 庆云县| 伊宁市| 会理县| 崇文区| 雷山县| 梁山县| 贡嘎县| 聂拉木县| 广西| 仙游县| 济源市| 武宁县| 民和| 永和县| 阜新| 伊吾县| 临沭县| 贵港市| 吉首市| 河津市| 神农架林区| 西峡县| 巴东县| 叶城县| 尼玛县|