Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class
ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class |
1.返回顶部 |
2.返回顶部 |
3.返回顶部 |
4.返回顶部 |
- package com.ylbtech.api.platform.util;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- import javax.validation.constraints.NotBlank;
- import java.time.Duration;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- /**
- * Redis工具
- *
- */
- @Component
- public class RedisUtils {
- @Resource private RedisTemplate<String, Object> redisTemplate;
- // =============================common============================
- /**
- * 设置缓存失效时间
- *
- * @param key 键
- * @param timeout 时间
- * @return {Boolean}
- */
- public Boolean setExpire(@NotBlank final String key, @NotBlank final Duration timeout) {
- if (timeout.getSeconds() > 0) {
- return this.redisTemplate.expire(key, timeout.getSeconds(), TimeUnit.SECONDS);
- }
- return false;
- }
- /**
- * 获取缓存失效时间
- *
- * @param key 键
- * @return 时间(秒) 0为永久有效
- */
- public Long getExpire(@NotBlank final String key) {
- return this.redisTemplate.getExpire(key, TimeUnit.SECONDS);
- }
- /**
- * key 是否存在
- *
- * @param key 键
- * @return {Boolean}
- */
- public Boolean hasKey(@NotBlank final String key) {
- return this.redisTemplate.hasKey(key);
- }
- /**
- * 删除缓存
- *
- * @param keys 键
- */
- public Boolean delete(@NotBlank final String... keys) {
- return keys.length
- == Optional.ofNullable(this.redisTemplate.delete(Arrays.asList(keys))).orElse(-1L);
- }
- // ============================String=============================
- /**
- * 获取普通缓存
- *
- * @param key 键
- * @return 值
- */
- public Object getValue(@NotBlank final String key) {
- return this.redisTemplate.opsForValue().get(key);
- }
- /**
- * 设置普通缓存
- *
- * @param key 键
- * @param value 值
- */
- public void setValue(@NotBlank final String key, @NotBlank final Object value) {
- this.redisTemplate.opsForValue().set(key, value);
- }
- /**
- * 设置普通缓存
- *
- * @param key 键
- * @param value 值
- * @param timeout 时间 小于等于0时将设为无限期
- */
- public void setValue(
- @NotBlank final String key, @NotBlank final Object value, @NotBlank final Duration timeout) {
- this.redisTemplate.opsForValue().set(key, value, timeout);
- }
- /**
- * 递增
- *
- * @param key 键
- * @param delta 要增加几(大于0)
- * @return 加上指定值之后 key 的值
- */
- public Long incrementValue(@NotBlank final String key, @NotBlank final long delta) {
- if (delta > 0) {
- throw new RuntimeException("递增因子必须大于0");
- }
- return this.redisTemplate.opsForValue().increment(key, delta);
- }
- /**
- * 递减
- *
- * @param key 键
- * @param delta 要减少几(小于0)
- * @return 减少指定值之后 key 的值
- */
- public Long decrementValue(@NotBlank final String key, @NotBlank final long delta) {
- if (delta < 0) {
- throw new RuntimeException("递减因子必须大于0");
- }
- return this.redisTemplate.opsForValue().increment(key, -delta);
- }
- // ================================Map=================================
- /**
- * HashGet
- *
- * @param key 键
- * @param item 项
- * @return 值
- */
- public Object getHash(@NotBlank final String key, @NotBlank final String item) {
- return this.redisTemplate.opsForHash().get(key, item);
- }
- /**
- * 获取hashKey对应的所有键值
- *
- * @param key 键
- * @return 对应的多个键值
- */
- public Map<Object, Object> getHash(@NotBlank final String key) {
- return this.redisTemplate.opsForHash().entries(key);
- }
- /**
- * HashSet
- *
- * @param key 键
- * @param map 对应多个键值
- */
- public void putHash(@NotBlank final String key, @NotBlank final Map<String, Object> map) {
- this.redisTemplate.opsForHash().putAll(key, map);
- }
- /**
- * HashSet 并设置时间
- *
- * @param key 键
- * @param map 对应多个键值
- * @param timeout 时间
- */
- public void putHash(
- @NotBlank final String key,
- @NotBlank final Map<String, Object> map,
- @NotBlank final Duration timeout) {
- this.redisTemplate.opsForHash().putAll(key, map);
- this.setExpire(key, timeout);
- }
- /**
- * 向一张hash表中放入数据,如果不存在将创建
- *
- * @param key 键
- * @param item 项
- * @param value 值
- */
- public void putHash(
- @NotBlank final String key, @NotBlank final String item, @NotBlank final Object value) {
- this.redisTemplate.opsForHash().put(key, item, value);
- }
- /**
- * 向一张hash表中放入数据,如果不存在将创建
- *
- * @param key 键
- * @param item 项
- * @param value 值
- * @param timeout 时间 注意:如果已存在的hash表有时间,这里将会替换原有的时间
- */
- public void putHash(
- @NotBlank final String key,
- @NotBlank final String item,
- @NotBlank final Object value,
- @NotBlank final Duration timeout) {
- this.redisTemplate.opsForHash().put(key, item, value);
- this.setExpire(key, timeout);
- }
- /**
- * 删除hash表中的值
- *
- * @param key 键
- * @param item 项
- */
- public void deleteHash(@NotBlank final String key, @NotBlank final Object... item) {
- this.redisTemplate.opsForHash().delete(key, item);
- }
- /**
- * 判断hash表中是否有该项的值
- *
- * @param key 键
- * @param item 项
- * @return {Boolean}
- */
- public Boolean hasKeyHash(@NotBlank final String key, @NotBlank final String item) {
- return this.redisTemplate.opsForHash().hasKey(key, item);
- }
- /**
- * hash递增 如果不存在,就会创建一个 并把新增后的值返回
- *
- * @param key 键
- * @param item 项
- * @param by 要增加几(大于0)
- * @return 加上指定值之后 key 的值
- */
- public Double incrementHash(
- @NotBlank final String key, @NotBlank final String item, @NotBlank final double by) {
- return this.redisTemplate.opsForHash().increment(key, item, by);
- }
- /**
- * hash递减
- *
- * @param key 键
- * @param item 项
- * @param by 要减少记(小于0)
- * @return 减少指定值之后 key 的值
- */
- public Double decrementHash(
- @NotBlank final String key, @NotBlank final String item, @NotBlank final double by) {
- return this.redisTemplate.opsForHash().increment(key, item, -by);
- }
- // ============================set=============================
- /**
- * 根据 key 获取 Set 中的所有值
- *
- * @param key 键
- * @return Set<Object>
- */
- public Set<Object> getSet(@NotBlank final String key) {
- return this.redisTemplate.opsForSet().members(key);
- }
- /**
- * 根据 value 从一个 set 中查询,是否存在
- *
- * @param key 键
- * @param value 值
- * @return {Boolean}
- */
- public Boolean hasKeySet(@NotBlank final String key, @NotBlank final Object value) {
- return this.redisTemplate.opsForSet().isMember(key, value);
- }
- /**
- * 将数据放入set缓存
- *
- * @param key 键
- * @param values 值
- * @return 放入个数
- */
- public Long addSet(@NotBlank final String key, @NotBlank final Object... values) {
- return this.redisTemplate.opsForSet().add(key, values);
- }
- /**
- * 将set数据放入缓存
- *
- * @param key 键
- * @param timeout 时间
- * @param values 值
- * @return 放入个数
- */
- public Long addSet(
- @NotBlank final String key,
- @NotBlank final Duration timeout,
- @NotBlank final Object... values) {
- final Long num = this.redisTemplate.opsForSet().add(key, values);
- this.setExpire(key, timeout);
- return num;
- }
- /**
- * 获取set缓存的长度
- *
- * @param key 键
- * @return 缓存的长度
- */
- public Long getSetSize(@NotBlank final String key) {
- return this.redisTemplate.opsForSet().size(key);
- }
- /**
- * 移除值为value的
- *
- * @param key 键
- * @param values 值
- * @return 移除个数
- */
- public Long removeSet(@NotBlank final String key, @NotBlank final Object... values) {
- return this.redisTemplate.opsForSet().remove(key, values);
- }
- // ===============================list=================================
- /**
- * 获取list缓存的内容
- *
- * @param key 键
- * @param start 开始
- * @param end 结束 0 到 -1代表所有值
- * @return list缓存的内容
- */
- public List<Object> getList(
- @NotBlank final String key, @NotBlank final Long start, @NotBlank final Long end) {
- return this.redisTemplate.opsForList().range(key, start, end);
- }
- /**
- * 获取list缓存的长度
- *
- * @param key 键
- * @return list缓存的长度
- */
- public Long getListSize(@NotBlank final String key) {
- return this.redisTemplate.opsForList().size(key);
- }
- /**
- * 通过索引 获取list中的值
- *
- * @param key 键
- * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
- * @return list中的值
- */
- public Object getListIndex(@NotBlank final String key, @NotBlank final Long index) {
- return this.redisTemplate.opsForList().index(key, index);
- }
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @return 放入个数
- */
- public Long pushList(@NotBlank final String key, @NotBlank final Object value) {
- return this.redisTemplate.opsForList().rightPush(key, value);
- }
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @param timeout 时间
- */
- public Long pushList(
- @NotBlank final String key, @NotBlank final Object value, @NotBlank final Duration timeout) {
- final Long num = this.redisTemplate.opsForList().rightPush(key, value);
- this.setExpire(key, timeout);
- return num;
- }
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @return 放入个数
- */
- public Long pushList(@NotBlank final String key, @NotBlank final List<Object> value) {
- return this.redisTemplate.opsForList().rightPushAll(key, value);
- }
- /**
- * 将list放入缓存
- *
- * @param key 键
- * @param value 值
- * @param timeout 时间
- * @return 放入个数
- */
- public Long pushList(
- @NotBlank final String key,
- @NotBlank final List<Object> value,
- @NotBlank final Duration timeout) {
- final Long num = this.redisTemplate.opsForList().rightPushAll(key, value);
- this.setExpire(key, timeout);
- return num;
- }
- /**
- * 根据索引修改 list 中的某条数据
- *
- * @param key 键
- * @param index 索引
- * @param value 值
- */
- public void updateListIndex(
- @NotBlank final String key, @NotBlank final Long index, @NotBlank final Object value) {
- this.redisTemplate.opsForList().set(key, index, value);
- }
- /**
- * 移除N个值为value
- *
- * @param key 键
- * @param count 移除多少个
- * @param value 值
- * @return 移除个数
- */
- public Long removeList(
- @NotBlank final String key, @NotBlank final Long count, @NotBlank final Object value) {
- return this.redisTemplate.opsForList().remove(key, count, value);
- }
- }
5.返回顶部 |
6.返回顶部 |
![]() |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class的更多相关文章
- Java魔法堂:深入正则表达式API
目录 一.前言 二.正则表达式的使用诉求 三.java.util.regex包 四.java.lang.String实例 五.最短路径实现诉求 六.Java支持的正则表达式功能语法 七.总结 八.参考 ...
- Expo大作战(二十七)--expo sdk api之Util(expo自带工具类),tackSnapshotAsync,Svg,SQLite
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- BMap:WEB 服务API
ylbtech-Map-Baidu: WEB 服务API 百度地图Web服务API为开发者提供http/https接口,即开发者通过http/https形式发起检索请求,获取返回json或xml格式的 ...
- Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage
ylbtech-Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage 1.返回顶部 1.1. pack ...
- Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- Java设计模式4:单例模式
前言 非常重要,单例模式是各个Java项目中必不可少的一种设计模式.本文的关注点将重点放在单例模式的写法以及每种写法的线程安全性上.所谓"线程安全性"的意思就是保证在创建单例对象的 ...
- Atitit 实现java的linq 以及与stream api的比较
Atitit 实现java的linq 以及与stream api的比较 1.1. Linq 和stream api的关系,以及主要优缺点1 1.2. Linq 与stream api的适用场景1 1. ...
- Java魔法堂:类加载器入了个门
一.前言 <Java魔法堂:类加载机制入了个门>中提及整个类加载流程中只有加载阶段作为码农的我们可以入手干预,其余均由JVM处理.本文将记录加载阶段的核心组件——类加载器的相关信息,以便日 ...
- Java并发编程:如何创建线程?
Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...
随机推荐
- 【运维】使用Serv-U搭建FTP服务器
1.先安装好Serv-U,并作为系统服务安装 2.打开Serv-U,新建一个域 3.添加用户 4.解决阿里云专有网络的一个问题 遇到一个情景:需要使用Serv-U进行FTP更新软件,其中使用PASV的 ...
- JavaScript--字符串与JSON对象相互转换
JSON.parse() 兼容性:Chrome,Firefox (Gecko) 3.5 (1.9.1),IE 8.0,Safari 4.0 JSON.parse('[1, 5, "false ...
- 20175223 MySQL
目录 完成结果 要求 1 :导入world.sql 要求 2 :CityWanna.java CityWanna.java 要求 3 :CountryWanna.java CountryWanna.j ...
- idea Maven 一键 mvn clean package
文章目录 方法一 方法二 方法一 方法二
- Linux下tomcat启动慢,阻塞
声明:本文为转载,请尊重版权,原文地址: https://www.cnblogs.com/songjinju/p/7505564.html 这两天在linux部署完tomcat以后,发现每次启动都非常 ...
- js实现图片预览、压缩、上传
先看几个对象:Blob.ArrayBuffer.File.fileReader.formData 详细解释请参考:https://www.cnblogs.com/youhong/p/10875190. ...
- 使用soapui进行webservice接口测试
一.web service(SOAP)与HTTP接口的区别 1.什么是web service WebService就是Web服务的意思,对应的应用层协议为SOAP(相当于HTTP协议),可理解为远 ...
- ldap认证服务的搭建
1. Ldap服务介绍 LDAP 全称轻量级目录访问协议(英文:Lightweight Directory Access Protocol),是一个运行在 TCP/IP 上的目录访问协议.目录是一个特 ...
- [Java Performance] 线程及同步的性能之线程池/ThreadPoolExecutors/ForkJoinPool
线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理 ...
- 线性方程组迭代算法——Gauss-Seidel迭代算法的python实现
原理: 请看本人博客:线性方程组的迭代求解算法——原理 代码: import numpy as np max=100#迭代次数上限 Delta=0.01 m=2#阶数:矩阵为2阶 n=3#维数:3X3 ...