获取redis连接服务
2016-07-30 14:54
441 查看
1.先下载redis.clients.jedis.Jedis jar包
import java.util.Set;
import redis.clients.jedis.Jedis;
public class RedisUtil {
private static Jedis jedis;
private static final String PREFIX = "redis-";
private static final String HOST_IP = "127.0.0.1";
private static final int HOST_PORT = 6379;
public synchronized static Jedis getJedis(String host_ip, int host_port) {
jedis = new Jedis(host_ip, host_port);
//jedis.auth("root"); //开启密码验证(配置文件中为 requirepass root)的时候需要执行该方法
return jedis;
}
public synchronized static Jedis getDefaultJedis() {
return getJedis(HOST_IP, HOST_PORT);
}
/**
* 清空 redis 中的所有数据
* */
public String flushRedis() {
return getDefaultJedis().flushDB();
}
/**
* 根据 pattern 获取 redis 中的键
* */
public Set<String> getKeysByPattern(String pattern) {
return getDefaultJedis().keys(pattern);
}
/**
* 获取 redis 中所有的键
* */
public Set<String> getAllKeys() {
return getKeysByPattern("*");
}
/**
* 判断key是否存在redis中
* */
public boolean exists(String key) throws Exception {
if (StringUtil.IsEmpty(key)) {
throw new Exception("key is null");
}
return getDefaultJedis().exists(PREFIX + key);
}
/**
* 从Redis中移除一个key
* */
public void removeKey(String key) throws Exception {
if (StringUtil.IsEmpty(key)) {
throw new Exception("key is null");
}
getDefaultJedis().del(PREFIX + key);
}
public static void main (String []args){
Jedis jedis = getDefaultJedis();
System.out.println("jedis is:"+jedis);
}
}
import java.util.Set;
import redis.clients.jedis.Jedis;
public class RedisUtil {
private static Jedis jedis;
private static final String PREFIX = "redis-";
private static final String HOST_IP = "127.0.0.1";
private static final int HOST_PORT = 6379;
public synchronized static Jedis getJedis(String host_ip, int host_port) {
jedis = new Jedis(host_ip, host_port);
//jedis.auth("root"); //开启密码验证(配置文件中为 requirepass root)的时候需要执行该方法
return jedis;
}
public synchronized static Jedis getDefaultJedis() {
return getJedis(HOST_IP, HOST_PORT);
}
/**
* 清空 redis 中的所有数据
* */
public String flushRedis() {
return getDefaultJedis().flushDB();
}
/**
* 根据 pattern 获取 redis 中的键
* */
public Set<String> getKeysByPattern(String pattern) {
return getDefaultJedis().keys(pattern);
}
/**
* 获取 redis 中所有的键
* */
public Set<String> getAllKeys() {
return getKeysByPattern("*");
}
/**
* 判断key是否存在redis中
* */
public boolean exists(String key) throws Exception {
if (StringUtil.IsEmpty(key)) {
throw new Exception("key is null");
}
return getDefaultJedis().exists(PREFIX + key);
}
/**
* 从Redis中移除一个key
* */
public void removeKey(String key) throws Exception {
if (StringUtil.IsEmpty(key)) {
throw new Exception("key is null");
}
getDefaultJedis().del(PREFIX + key);
}
public static void main (String []args){
Jedis jedis = getDefaultJedis();
System.out.println("jedis is:"+jedis);
}
}
相关文章推荐
- SpringDataRedis事务 专题
- redis入门--redis安装
- Linux下Redis-3.0.7版本的安装以及Redis主备的部署(一)
- Spring Data Redis 使用
- redis配置参数说明
- elasticsearch+logstash+redis+kibana+logback整合
- redis 对基本数据类型的处理接口
- JCS和Redis
- redis 入门介绍
- Java连接Redis
- Redis 集群方案
- Redis安装及Java客户端的使用浅析(jedis)
- asp.net redis 实战
- 在Windows下为PHP5.5安装redis扩展
- redis超时原因系统性排查
- Ubuntu下如何安装与运行Redis
- redis多端口启动
- Redis配置文件参数说明
- Redis 存储List对象
- 跟我学Redis(4)—Redis配置文件redis.conf