java web 中如何使用redis,Java Web中使用 Jedis 操作 Redis 数据库

现在的redis这种内存型数据库,已经成为大型项目的必须,与很老的session存数据相比,他有很多优势。在Java中,一般使用 Jedis 扩展来使用redis数据库。

引入jar包

redis.clients

jedis

2.9.0

代码

数据库连接类(因为项目上线以后,可能很多参数是需要修改的,建议使用配置文件的方式配置相关参数,这样便于配置参数的修改):

public class RedisProvider {

protected static final Logger logger = Logger.getLogger(RedisProvider.class);

protected static JedisPool jedispool;

private static PropertiesConfiguration properties = null;

protected static int EXPIRE = 120;

static{

try {

properties = new PropertiesConfiguration("cache/redis.properties");

} catch (ConfigurationException e) {

logger.error("配置文件未加载成功");

}

JedisPoolConfig jedisconfig = new JedisPoolConfig();

jedisconfig.setMaxTotal(Integer.valueOf(properties

.getString("redis.pool.maxTotal")));

jedisconfig.setMaxIdle(Integer.valueOf(properties

.getString("redis.pool.maxIdle")));

jedisconfig.setMaxWaitMillis(Long.valueOf(properties

.getString("redis.pool.maxWait")));

jedisconfig.setTestOnBorrow(Boolean.valueOf(properties

.getString("redis.pool.testOnBorrow")));

jedisconfig.setTestOnReturn(Boolean.valueOf(properties

.getString("redis.pool.testOnReturn")));

jedispool = new JedisPool(jedisconfig, properties.getString("redis.ip"),

Integer.valueOf(properties.getString("redis.port")), 100000);

}

public static Jedis getJedis() {

Jedis jedis = null;

try {

jedis = jedispool.getResource();

} catch (JedisConnectionException jce) {

logger.warn("获取连接失败:" + jce.getMessage());

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

logger.warn("线程睡眠失败:" + e.getMessage());

}

jedis = jedispool.getResource();

}

return jedis;

}

public static void closeJedis(Jedis jedis) {

if (jedis != null) {

jedis.close();

}

}

}

数据操作类(简单的写了几个示例):

public class RedisHelper extends RedisProvider {

private static final Logger logger = Logger.getLogger(RedisHelper.class);

/**

* 保存键值对,过期时间为默认值(默认2分钟)

* @param key

* @param value

*/

public static void set(String key, String value) {

Jedis jedis = null;

try {

jedis = getJedis();

jedis.setex(key, EXPIRE, value);

} catch (Exception e) {

logger.error("默认时间set:" + e.getMessage());

} finally {

closeJedis(jedis);

}

}

/**

* 保存键值对,自定义过期时间(单位s)

* @param key

* @param value

* @param expire

*/

public static void set(String key, String value, int expire) {

Jedis jedis = null;

try {

jedis = getJedis();

jedis.setex(key, expire, value);

} catch (Exception e) {

logger.error("自定义时间set:" + e.getMessage());

} finally {

closeJedis(jedis);

}

}

/**

* 根据key获取value

* @param key

* @return

*/

public static String get(String key) {

Jedis jedis = null;

String value = null;

try {

jedis = getJedis();

value = jedis.get(key);

} catch (Exception e) {

logger.error("根据key获取value:" + e.getMessage());

} finally {

closeJedis(jedis);

return value;

}

}

/**

* 删除键值对,根据所提供的key

* @param key

*/

public static void clear(String key) {

Jedis jedis = null;

try {

jedis = getJedis();

jedis.del(key);

} catch (Exception e) {

logger.error("根据key删除值:" + e.getMessage());

} finally {

closeJedis(jedis);

}

}

/**

* 检查值是否存在

* @param key

* @return

*/

public static boolean exists(String key) {

Jedis jedis = null;

boolean bool = false;

try {

jedis = getJedis();

bool = jedis.exists(key);

} catch (Exception e) {

logger.error("判断值是否存在" + e.getMessage());

} finally {

closeJedis(jedis);

return bool;

}

}

}

0

相关

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值