spring配置文件详解_jedis连接redis集群

spring配置文件详解_jedis连接redis集群[b]JedisCache[/b][code="java"]importjava.io.IOException;importjava.util.Set;importorg.springframework.beans.factory.InitializingBean;importorg.springframework.util.Assert;importre…


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>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197239.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 最详细的Vuex教程

    最详细的Vuex教程最详细的Vuex教程什么是Vuex?vuex是一个专门为vue.js设计的集中式状态管理架构。状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态。简单的说就是data中需要共用的属性。引入Vuex(前提是已经用Vue脚手架工具构建好项目)1、利用npm包管理工具,进行安装vuex。在控制命令行中输入下边的命令就可以了。npminstallvuex

    2022年6月9日
  • centos7系统更新命令_centos 更新

    centos7系统更新命令_centos 更新1.查看网络IP ifconfig2.下载命令 wget+网址3.安装 yum-y install + 目标4.删除文件 sudo rm 文件所在目录/目标强制删除文件 rm -f删除目录 rm -rf5.复制一个文件到另一个文件夹sudo cp /文件夹/文件 /另一个文件夹6.对一些文件进行读写sudo vim 文件名7….

    2022年8月18日
  • Linux学习(CentOS-7)—磁盘分区(概念、分区方法、分区方案)

    Linux学习(CentOS-7)—磁盘分区(概念、分区方法、分区方案)

    2021年9月23日
  • 关于范式的一些简单理解

    关于范式的一些简单理解

    2021年11月24日
  • idea tomcat localhost log 乱码_idea tomcat 乱码

    idea tomcat localhost log 乱码_idea tomcat 乱码解决idea日志乱码和tomcat日志乱码问题1idea启动tomcat乱码遇到的问题:idea整合SSM项目,tomcat启动,启动时查看idea上的tomcat日志,出现乱码:但是直接用tomcat的startup.bat启动,无乱码:出现的原因:查看tomcat/conf/logging.propertiesjava.util.logging.ConsoleHandler.encoding=GBK而本window系统使用的也是GBK编码,所以直接使用bat文件启动正常,

    2022年9月26日
  • pycharm选择运行环境_安卓漂亮的界面

    pycharm选择运行环境_安卓漂亮的界面手把手教你配置最漂亮的PyCharm界面,Python程序员必备!简介:高逼格超美的IDE界面,是每个程序员的梦想!随着人工智能/机器学习的兴起,Python作为一门“漂亮的语言”,再次获得广大程序员的关注。而JetBrains出品的PyCharm无疑是最好用的PythonIDE之一。俗话说“工欲善其事,必先利其器”,把自己的IDE配置得既有逼格又好看,这是每个Python程序员必备的技能。推荐给大家一个学习交流的地方:719+139+688,本文就手把手的教你,如何把自己的PyCharm配置的

    2022年8月28日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号