写一个java版本的redis各种数据类型的工具类
时间: 2024-02-11 17:05:20 浏览: 147
JAVA Redis的工具类
好的,以下是一个基于Java的Redis各种数据类型的工具类示例:
```java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class RedisUtil {
private static JedisPool jedisPool = null;
static {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxIdle(20);
jedisPoolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379);
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
public static void returnJedis(Jedis jedis) {
jedisPool.returnResource(jedis);
}
/**
* String类型
*/
public static void set(String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnJedis(jedis);
}
}
public static String get(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
returnJedis(jedis);
}
}
/**
* List类型
*/
public static void lpush(String key, String... values) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.lpush(key, values);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnJedis(jedis);
}
}
public static List<String> lrange(String key, long start, long end) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.lrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
returnJedis(jedis);
}
}
/**
* Set类型
*/
public static void sadd(String key, String... members) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.sadd(key, members);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnJedis(jedis);
}
}
public static Set<String> smembers(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.smembers(key);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
returnJedis(jedis);
}
}
/**
* Hash类型
*/
public static void hset(String key, String field, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.hset(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnJedis(jedis);
}
}
public static String hget(String key, String field) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.hget(key, field);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
returnJedis(jedis);
}
}
public static Map<String, String> hgetAll(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.hgetAll(key);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
returnJedis(jedis);
}
}
/**
* Zset类型
*/
public static void zadd(String key, double score, String member) {
Jedis jedis = null;
try {
jedis = getJedis();
jedis.zadd(key, score, member);
} catch (Exception e) {
e.printStackTrace();
} finally {
returnJedis(jedis);
}
}
public static Set<String> zrange(String key, long start, long end) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.zrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
returnJedis(jedis);
}
}
}
```
这个工具类中包含了如何初始化Jedis连接池、获取、释放Jedis连接以及常用的String、List、Set、Hash、Zset数据类型的操作。您可以在使用该工具类时,根据需要进行调整和扩展。
阅读全文