Java中redis的简单使用
作者:阳旭网络
环境依赖:需下载安装redis
一、在项目的pom.xml中引入jar包
一、在项目的pom.xml中引入jar包
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
二、写一个工具类
package com.yx.cus.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis工具类
*@author HHR
*@date 2017年8月28日 下午7:54:17
*/
public class JedisClient{
private static final Logger logger = LoggerFactory.getLogger(JedisClient.class);
private static JedisPool jedisPool = null;
private static Jedis jedis = null;
public static Jedis jedis_object = null;
// @Value("${spring.redis.host}")
// private static String host;
// @Value("${spring.redis.password}")
// private static String password;
// @Value("${spring.redis.port}")
// private static Integer port;
// private static String host = "47.106.10.57";
private static String host = "192.168.0.123";//redis配置的ip地址
// private static String password = "9drur!@34";
private static Integer port = 6379;//redis配置的端口号
static{
if(jedisPool == null){
JedisPoolConfig config = new JedisPoolConfig();
//设置最大连接数
config.setMaxTotal(500);
//设置最大空闲数
config.setMaxIdle(20);
//设置最小空闲数
config.setMinIdle(8);
//设置超时时间
config.setMaxWaitMillis(3000);
//Idle时进行连接扫描
//config.setTestWhileIdle(true);
//表示idle object evitor两次扫描之间要sleep的毫秒数
//config.setTimeBetweenEvictionRunsMillis(30000);
//表示idle object evitor每次扫描的最多的对象数
//config.setNumTestsPerEvictionRun(10);
//表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
//config.setMinEvictableIdleTimeMillis(60000);
//初始化连接池
jedisPool = new JedisPool(config, host, port);
jedis_object = new Jedis( host, port);
}
}
private JedisClient() {
}
private static Jedis getJedisInstance(){
try {
if(null == jedis){
jedis = jedisPool.getResource();
// jedis.auth(password);
}
} catch (Exception e) {
logger.error("实例化jedis失败.........", e);
}
return jedis;
}
/**
* 向缓存中设置字符串内容
*@author HHR
*@date 2017年8月29日 下午3:19:41
*/
public static boolean set(String key, String value) throws Exception {
Jedis jedis = null;
try {
jedis = getJedisInstance();
jedis.set(key, value);
return true;
} catch (Exception e) {
logger.error("redis set方法失败...key="+key+" value="+value, e);
} finally {
jedisPool.close();
}
return false;
}
/**
* 向缓存中设置字符串内容 ,设置过期时间
*@author HHR
*@date 2017年8月29日 下午3:19:41
*/
public static boolean set(String key, String value,Integer seconds) throws Exception {
Jedis jedis = null;
try {
jedis = getJedisInstance();
jedis.set(key, value);
jedis.expire(key, seconds);
return true;
} catch (Exception e) {
logger.error("redis set方法失败...key="+key+" value="+value, e);
} finally {
jedisPool.close();
}
return false;
}
/**
* 根据key 获取内容
*@author HHR
*@date 2017年8月29日 下午3:19:47
*/
public static Object get(String key) {
Jedis jedis = null;
try {
jedis = getJedisInstance();
Object value = jedis.get(key);
return value;
} catch (Exception e) {
logger.error("redis get方法失败...key="+key);
} finally {
jedisPool.close();
}
return null;
}
/**
* 删除缓存中得对象,根据key
*@author HHR
*@date 2017年8月29日 下午3:19:53
*/
public static boolean del(String key) {
Jedis jedis = null;
try {
jedis = getJedisInstance();
jedis.del(key);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisPool.close();
}
return false;
}
/**
* 根据key 获取对象
*@author HHR
*@date 2017年8月29日 下午3:19:58
*/
public static <T> T get(String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = getJedisInstance();
String value = jedis.get(key);
return JSON.parseObject(value, clazz);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisPool.close();
}
return null;
}
/**
* 设置key过期
*@author HHR
*@date 2017年8月29日 下午3:20:03
*/
public static boolean expire(String key,int seconds){
Jedis jedis = null;
try {
jedis = getJedisInstance();
jedis.expire(key, seconds);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisPool.close();
}
return false;
}
/**
* list push
*@author HHR
*@date 2017年8月29日 下午3:20:10
*/
public static boolean lpush(String key,String value){
Jedis jedis = null;
try {
jedis = getJedisInstance();
jedis.lpush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisPool.close();
}
return false;
}
/**
* list lpop
*@author HHR
*@date 2017年8月29日 下午3:20:14
*/
public static String lpop(String key){
Jedis jedis = null;
try {
jedis = getJedisInstance();
return jedis.lpop(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisPool.close();
}
return null;
}
/**
* list rpush
*@author HHR
*@date 2017年8月29日 下午3:20:20
*/
public static boolean rpush(String key,String value){
Jedis jedis = null;
try {
jedis = getJedisInstance();
jedis.lpush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisPool.close();
}
return false;
}
/**
* list rpop
*@author HHR
*@date 2017年8月29日 下午3:20:27
*/
public static String rpop(String key){
Jedis jedis = null;
try {
jedis = getJedisInstance();
return jedis.rpop(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisPool.close();
}
return null;
}
/**
* 散列添加
*@author HHR
*@date 2017年8月29日 下午3:20:32
*/
public static boolean hsetAddField(String key,String fieldName,String value){
Jedis jedis = null;
try {
jedis = getJedisInstance();
jedis.hset(key, fieldName, value);
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
jedisPool.close();
}
return false;
}
/**
* 用于将键的整数值递增1
*@author HHR
*@date 2017年8月29日 下午3:21:23
*/
public static Long incr(String key){
Jedis jedis = null;
try {
jedis = getJedisInstance();
return jedis.incr(key);
} catch (Exception e) {
e.printStackTrace();
return -100l;
}finally {
jedisPool.close();
}
}
/**
* 判断是否存在key
*@author HHR
*@date 2017年8月29日 下午3:20:42
*/
public static Boolean exists(String key){
Jedis jedis = null;
try {
jedis = getJedisInstance();
return jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
jedisPool.close();
}
}
}
1.需要注意的就是两段红色字体的的代码,一个是ip地址,一个是端口号,需要和redis配置文件里的一致。
2.ip地址一般默认是127.0.0.1,端口号默认是6379。
3.可以在redis安装目录下的redis.conf(有些是redis.windows.conf)中修改
如:bind 127.0.0.1
port 6379
也可以配置多个ip,空格隔开就行
如:bind 127.0.0.1 192.168.0.123
*
**注意:修改后启动redis,一定要启动指定的conf配置文件,
如:在命令行窗口切换到redis的安装目录下输入redis-server.exe redis.windows.conf,不能输入redis-server &,因为这样启动的是默认的配置*
三、主要使用的几个方法
1.存数据:
方法一:set(String key,String val)这个方法需要输入两个参数,键和值
方法二:set(String key,String val,Integer seconds)该方法比方法一多了一个参数“过期时间”,以秒位单位
2.取数据:
通过调用工具类的get(String key)方法来获取数据
3.删除缓存中的对象:
调用del(String key)方法删