java redis工具类_spring boot结合Redis实现工具类的方法示例

自己整理了 spring boot 结合 Redis 的工具类

引入依赖

org.springframework.boot

spring-boot-starter-data-redis

加入配置

# Redis数据库索引(默认为0)

spring.redis.database=0

# Redis服务器地址

spring.redis.host=localhost

# Redis服务器连接端口

spring.redis.port=6379

实现代码

这里用到了 静态类工具类中 如何使用 @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 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 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 keys) {

return cacheUtils.redisTemplate.delete(keys);

}

}

相关地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值