大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
[b]JedisCache[/b]
import java.io.IOException;
import java.util.Set;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.my.utils.serialization.KryoSerializationUtils;
public class JedisCache implements InitializingBean {
private JedisPool jedisPool;
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
public void putObject(String key, Object object) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key.getBytes(), KryoSerializationUtils.serialize(object));
}
catch (Exception ex) {
handleException(ex, jedisPool, jedis);
}
finally {
if (jedis != null && jedis.isConnected()) {
jedisPool.returnResource(jedis);
}
}
}
public void putObject(String key, Object object, int expirationInSeconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.setex(key.getBytes(), expirationInSeconds, KryoSerializationUtils.serialize(object));
}
catch (Exception ex) {
handleException(ex, jedisPool, jedis);
}
finally {
if (jedis != null && jedis.isConnected()) {
jedisPool.returnResource(jedis);
}
}
}
public Object getObject(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return (Object) KryoSerializationUtils.deserialize(jedis.get(key.getBytes()));
}
catch (Exception ex) {
handleException(ex, jedisPool, jedis);
}
finally {
if (jedis != null && jedis.isConnected()) {
jedisPool.returnResource(jedis);
}
}
return null;
}
public void removeObject(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(key.getBytes());
}
catch (Exception ex) {
handleException(ex, jedisPool, jedis);
}
finally {
if (jedis != null && jedis.isConnected()) {
jedisPool.returnResource(jedis);
}
}
}
public boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key.getBytes());
}
catch (Exception ex) {
handleException(ex, jedisPool, jedis);
}
finally {
if (jedis != null && jedis.isConnected()) {
jedisPool.returnResource(jedis);
}
}
return false;
}
public long expire(String key, int expirationInSeconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.expire(key.getBytes(), expirationInSeconds);
}
catch (Exception ex) {
handleException(ex, jedisPool, jedis);
}
finally {
if (jedis != null && jedis.isConnected()) {
jedisPool.returnResource(jedis);
}
}
return 0L;
}
public Set<String> keys(final String pattern) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.keys(pattern);
}
catch (Exception ex) {
handleException(ex, jedisPool, jedis);
}
finally {
if (jedis != null && jedis.isConnected()) {
jedisPool.returnResource(jedis);
}
}
return null;
}
/**
* 运行时异常,IO异常,销毁jedis对象
*
* @param ex
* @param jedisPool
* @param jedis
*/
private void handleException(Exception ex, JedisPool jedisPool, Jedis jedis) {
if (jedis == null)
throw new NullPointerException("jedis is null, please check the redis server.");
if (ex instanceof IOException) {
jedisPool.returnBrokenResource(jedis); //销毁该对象
}
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(jedisPool);
}
}
[b]application-bean.xml[/b]
<util:properties id="redisProps" location="#{T(java.lang.System).getProperty('config.local') != null ? 'classpath:redis.properties' : 'file:nfs/toc/redis.properties'}"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="#{redisProps['${profiles.active}.redis.maxTotal']}"/> <!-- 控制一个pool可分配多少个jedis实例 -->
<property name="maxIdle" value="#{redisProps['${profiles.active}.redis.maxIdle']}" /> <!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->
<property name="maxWaitMillis" value="#{redisProps['${profiles.active}.redis.maxWaitMillis']}" /> <!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->
<property name="testOnBorrow" value="#{redisProps['${profiles.active}.redis.testOnBorrow']}" /> <!-- 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 -->
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="#{redisProps['${profiles.active}.redis.server.host']}" />
<constructor-arg name="port" value="#{redisProps['${profiles.active}.redis.server.port']}" />
</bean>
<bean id="jedisCache" class="com.my.cache.redis.JedisCache">
<property name="jedisPool" ref="jedisPool"/>
</bean>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/197239.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...