spring boot 结合Redis 实现工具类
自己整理了 spring boot 结合 Redis 的工具类
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
加入配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
推荐一个交流学习群:614478470 里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多
点击:加入
实现代码
这里用到了 静态类工具类中 如何使用 @Autowired
package com.lmxdawn.api.common.utils;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
* 缓存操作类
*/
@Component
public class CacheUtils {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 维护一个本类的静态变量
private static CacheUtils cacheUtils;
@PostConstruct
public void init() {
cacheUtils = this;
cacheUtils.redisTemplate = this.redisTemplate;
}
/**
* 将参数中的字符串值设置为键的值,不设置过期时间
* @param key
* @param value 必须要实现 Serializable 接口
*/
public static void set(String key, String value) {
cacheUtils.redisTemplate.opsForValue().set(key, value);
}
/**
* 将参数中的字符串值设置为键的值,设置过期时间
* @param key
* @param value 必须要实现 Serializable 接口
* @param timeout
*/
public static void set(String key, String value, Long timeout) {
cacheUtils.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
* 获取与指定键相关的值
* @param key
* @return
*/
public static Object get(String key) {
return cacheUtils.redisTemplate.opsForValue().get(key);
}
/**
* 设置某个键的过期时间
* @param key 键值
* @param ttl 过期秒数
*/
public static boolean expire(String key, Long ttl) {
return cacheUtils.redisTemplate.expire(key, ttl, TimeUnit.SECONDS);
}
/**
* 判断某个键是否存在
* @param key 键值
*/
public static boolean hasKey(String key) {
return cacheUtils.redisTemplate.hasKey(key);
}
/**
* 向集合添加元素
* @param key
* @param value
* @return 返回值为设置成功的value数
*/
public static Long sAdd(String key, String... value) {
return cacheUtils.redisTemplate.opsForSet().add(key, value);
}
/**
* 获取集合中的某个元素
* @param key
* @return 返回值为redis中键值为key的value的Set集合
*/
public static Set<String> sGetMembers(String key) {
return cacheUtils.redisTemplate.opsForSet().members(key);
}
/**
* 将给定分数的指定成员添加到键中存储的排序集合中
* @param key
* @param value
* @param score
* @return
*/
public static Boolean zAdd(String key, String value, double score) {
return cacheUtils.redisTemplate.opsForZSet().add(key, value, score);
}
/**
* 返回指定排序集中给定成员的分数
* @param key
* @param value
* @return
*/
public static Double zScore(String key, String value) {
return cacheUtils.redisTemplate.opsForZSet().score(key, value);
}
/**
* 删除指定的键
* @param key
* @return
*/
public static Boolean delete(String key) {
return cacheUtils.redisTemplate.delete(key);
}
/**
* 删除多个键
* @param keys
* @return
*/
public static Long delete(Collection<String> keys) {
return cacheUtils.redisTemplate.delete(keys);
}
}
spring boot 结合Redis 实现工具类的更多相关文章
- spring boot 文件上传工具类(bug 已修改)
以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...
- Spring Boot使用Redis进行消息的发布订阅
今天来学习如何利用Spring Data对Redis的支持来实现消息的发布订阅机制.发布订阅是一种典型的异步通信模型,可以让消息的发布者和订阅者充分解耦.在我们的例子中,我们将使用StringRedi ...
- spring boot集成redis实现session共享
1.pom文件依赖 <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframe ...
- Spring Boot + Mybatis + Redis二级缓存开发指南
Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...
- Spring Boot2 系列教程(二十六)Spring Boot 整合 Redis
在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...
- 【spring boot】【redis】spring boot基于redis的LUA脚本 实现分布式锁
spring boot基于redis的LUA脚本 实现分布式锁[都是基于redis单点下] 一.spring boot 1.5.X 基于redis 的 lua脚本实现分布式锁 1.pom.xml &l ...
- 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!
转载自:https://github.com/whvcse/RedisUtil 代码 ProtoStuffSerializerUtil.java import java.io.ByteArrayInp ...
- SpringBoot入门系列(七)Spring Boot整合Redis缓存
前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...
- 玩转spring boot——结合redis
一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...
随机推荐
- ASP.NET MVC5+EF6+EasyUI 后台管理系统--系统权限及操作指引
系列目录 1.权限包括菜单权限,按钮权限,数据权限 2.角色组和用户之间是多对多的关系,即多个用户可以拥有多个角色组,权限是拥有角色组的并集 1.菜单界面,菜单都是动态数据由模块管理进行设置 2.权限 ...
- python class 2
//test.py 1 class Employee: 2 'all employee' 3 empCount = 0 4 def __init__(s ...
- ROSETTA使用技巧随笔--score.sc处理
对score.sc的便利操作: $ sort -n -k2 example_score_file.sc 此命令会以score文件的第二列进行排序(sort -n -k2,-n表示对数值型数据排序,可用 ...
- Xcode 常用命令
一些自己在开发过程中总结的命令,并不是完整的,会不断的更新. 1.图片转png格式 sips -s format png start.jpg --out StartBg.png 转换时,先cd 当前图 ...
- iOS 新浪微博-1.1框架升级
在iOS 新浪微博-1.0框架搭建 中,使用的是xcode5.1.1开发.现在把重整了一下框架 改为xcode7.0开发 使用cocoaPad管理第三方库 程序将托管到github上 在改为xcode ...
- Xcode 运行 Signing for "XXXXXX" requires selecting either a development team or a provisioning profile. Select a development team or a provisioning profile in the project editor
Signing for "XXXXXX" requires selecting either a development team or a provisioning profil ...
- cocos2dx JS 清除缓存重新编译打包安卓apk
复制他人工程时打包出错,无法进行.或者是资源缓存问题需要重新编译删除 proj.android 工程下的三个文件夹 frameworks -> runtime-src -> proj.an ...
- node.js初识04
node的Get表单提交 form.html <!DOCTYPE html> <html lang="en"> <head> <meta ...
- InstallShield2015制作安装包----------安装后实现电脑开机自启动
开机自启动有两个方法: 一 .把程序的快捷方式放在”开始---启动“目录下. 二.把程序的安装目录放在注册表”“. 实现方法一: 1.编写bat脚本.执行bat启动exe. a)核心:cmd命令 : ...
- c3p0:Connections could not be acquired from the underlying database!解决方案
在利用ssh框架做网站的时候遇到了一个比较棘手的问题,一直连接不上数据库,问题描述如下: 各种百度然后说的最多的解决方案是: 1,驱动配置有误:2,数据库连接地址有误:3,密码或帐号有误: 4,数据库 ...