redis封装类java_Java:封装 Redis 工具类

首先是一个定义 Redis 的 Key 接口类,记录 key 值(前缀)与缓存的时间:

public interface KeyPrefix {

public int expireSeconds();

public String getPrefix();

}

然后定义 Redis的工具类:

import java.util.ArrayList;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.ScanParams;

import redis.clients.jedis.ScanResult;

@Service

public class RedisService {

@Autowired

JedisPool jedisPool;

/**

* 获取单个对象

* */

public T get(KeyPrefix prefix, String key, Class clazz) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

//生成真正的key

String realKey = prefix.getPrefix() + key;

String str = jedis.get(realKey);

T t = stringToBean(str, clazz);

return t;

}finally {

returnToPool(jedis);

}

}

/**

* 设置对象

* */

public boolean set(KeyPrefix prefix, String key, T value) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

String str = beanToString(value);

if(str == null || str.length() <= 0) {

return false;

}

//生成真正的key

String realKey = prefix.getPrefix() + key;

int seconds = prefix.expireSeconds();

if(seconds <= 0) {

jedis.set(realKey, str);

}else {

jedis.setex(realKey, seconds, str);

}

return true;

}finally {

returnToPool(jedis);

}

}

/**

* 判断key是否存在

* */

public boolean exists(KeyPrefix prefix, String key) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

//生成真正的key

String realKey = prefix.getPrefix() + key;

return jedis.exists(realKey);

}finally {

returnToPool(jedis);

}

}

/**

* 删除

* */

public boolean delete(KeyPrefix prefix, String key) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

//生成真正的key

String realKey = prefix.getPrefix() + key;

long ret = jedis.del(realKey);

return ret > 0;

}finally {

returnToPool(jedis);

}

}

/**

* 增加值

* */

public Long incr(KeyPrefix prefix, String key) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

//生成真正的key

String realKey = prefix.getPrefix() + key;

return jedis.incr(realKey);

}finally {

returnToPool(jedis);

}

}

/**

* 减少值

* */

public Long decr(KeyPrefix prefix, String key) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

//生成真正的key

String realKey = prefix.getPrefix() + key;

return jedis.decr(realKey);

}finally {

returnToPool(jedis);

}

}

/**

* 删除值

* */

public boolean delete(KeyPrefix prefix) {

if(prefix == null) {

return false;

}

List keys = scanKeys(prefix.getPrefix());

if(keys==null || keys.size() <= 0) {

return true;

}

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

jedis.del(keys.toArray(new String[0]));

return true;

} catch (final Exception e) {

e.printStackTrace();

return false;

} finally {

if(jedis != null) {

jedis.close();

}

}

}

public List scanKeys(String key) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

List keys = new ArrayList();

String cursor = "0";

ScanParams sp = new ScanParams();

sp.match("*"+key+"*");

sp.count(100);

do{

ScanResult ret = jedis.scan(cursor,

sp);

List result = ret.getResult();

if(result!=null && result.size() > 0){

keys.addAll(result);

}

//再处理cursor

cursor = ret.getStringCursor();

}while(!cursor.equals("0"));

return keys;

} finally {

if (jedis != null) {

jedis.close();

}

}

}

public static String beanToString(T value) {

if(value == null) {

return null;

}

Class> clazz = value.getClass();

if(clazz == int.class || clazz == Integer.class) {

return ""+value;

}else if(clazz == String.class) {

return (String)value;

}else if(clazz == long.class || clazz == Long.class) {

return ""+value;

}else {

return JSON.toJSONString(value);

}

}

@SuppressWarnings("unchecked")

public static T stringToBean(String str, Class clazz) {

if(str == null || str.length() <= 0 || clazz == null) {

return null;

}

if(clazz == int.class || clazz == Integer.class) {

return (T)Integer.valueOf(str);

}else if(clazz == String.class) {

return (T)str;

}else if(clazz == long.class || clazz == Long.class) {

return (T)Long.valueOf(str);

}else {

return JSON.toJavaObject(JSON.parseObject(str), clazz);

}

}

private void returnToPool(Jedis jedis) {

if(jedis != null) {

jedis.close();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JedisUtil是一个Java Redis缓存工具类,它封装Jedis客户端的基本操作,使得使用Redis缓存更加简单方便。 以下是JedisUtil的示例代码: ``` import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisUtil { private static JedisPool jedisPool; static { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(1000); jedisPoolConfig.setMaxIdle(100); jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379); } public static void set(String key, String value) { try (Jedis jedis = jedisPool.getResource()) { jedis.set(key, value); } } public static String get(String key) { try (Jedis jedis = jedisPool.getResource()) { return jedis.get(key); } } public static void del(String key) { try (Jedis jedis = jedisPool.getResource()) { jedis.del(key); } } public static void expire(String key, int seconds) { try (Jedis jedis = jedisPool.getResource()) { jedis.expire(key, seconds); } } public static boolean exists(String key) { try (Jedis jedis = jedisPool.getResource()) { return jedis.exists(key); } } } ``` 在上面的代码中,我们使用了JedisPool来管理Jedis连接,它的作用是维护一定数量的Jedis连接,以便在需要时从池中获取连接,减少了创建和关闭连接的开销。 在使用JedisUtil时,我们只需要调用set、get、del、expire和exists等方法,就可以完成对Redis缓存的操作。 例如,要将一个键值对("name", "Tom")存入Redis中,可以使用以下代码: ``` JedisUtil.set("name", "Tom"); ``` 要获取键为"name"的值,可以使用以下代码: ``` String name = JedisUtil.get("name"); ``` 同时,JedisUtil还提供了删除、设置过期时间和判断键是否存在等方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值