详解java之redis篇(spring-data-redis整合)

时间:2021-05-19

1,利用spring-data-redis整合

项目使用的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ponent-scan> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="50" /> <property name="maxIdle" value="8" /> <property name="maxWait" value="1000" /> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="true"/> <!-- <property name="testWhileIdle" value="true"/> --> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="${redis.host}" /> <constructor-arg name="port" value="${redis.port}" /> <constructor-arg name="timeout" value="${redis.timeout}" /> <constructor-arg name="weight" value="1" /> </bean> </list> </constructor-arg> </bean></beans>

RedisDataSource:定义三个方法

public interface RedisDataSource { public abstract ShardedJedis getRedisClient(); public void returnResource(ShardedJedis shardedJedis); public void returnResource(ShardedJedis shardedJedis,boolean broken);}

实现redisDataSource:

1, 注入配置好的ShardedJedisPool,这三个方法的作用:

  • getRedisClient() : 取得redis的客户端,可以执行命令了。
  • returnResource(ShardedJedis shardedJedis) : 将资源返还给pool
  • returnResource(ShardedJedis shardedJedis, boolean broken) : 出现异常后,将资源返还给pool (其实不需要第二个方法)
@Repository("redisDataSource")public class RedisDataSourceImpl implements RedisDataSource { private static final Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class); @Autowired private ShardedJedisPool shardedJedisPool; public ShardedJedis getRedisClient() { try { ShardedJedis shardJedis = shardedJedisPool.getResource(); return shardJedis; } catch (Exception e) { log.error("getRedisClent error", e); } return null; } public void returnResource(ShardedJedis shardedJedis) { shardedJedisPool.returnResource(shardedJedis); } public void returnResource(ShardedJedis shardedJedis, boolean broken) { if (broken) { shardedJedisPool.returnBrokenResource(shardedJedis); } else { shardedJedisPool.returnResource(shardedJedis); } }}

第二层的封装:RedisClientTemplate,例子实现了放值和取值。最后代码提供了全部命令的实现。

代码就是映射性质的又一次调用jedis的方法而已,用了个broken来做标示符,决定返还资源的方式。

这一层的目的主要也是让再上层的调用不需要关心pool中链接的取得和返还问题了。

@Repository("redisClientTemplate")public class RedisClientTemplate { private static final Logger log = LoggerFactory.getLogger(RedisClientTemplate.class); @Autowired private RedisDataSource redisDataSource; public void disconnect() { ShardedJedis shardedJedis = redisDataSource.getRedisClient(); shardedJedis.disconnect(); } /** * 设置单个值 * * @param key * @param value * @return */ public String set(String key, String value) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.set(key, value); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; } /** * 获取单个值 * * @param key * @return */ public String get(String key) { String result = null; ShardedJedis shardedJedis = redisDataSource.getRedisClient(); if (shardedJedis == null) { return result; } boolean broken = false; try { result = shardedJedis.get(key); } catch (Exception e) { log.error(e.getMessage(), e); broken = true; } finally { redisDataSource.returnResource(shardedJedis, broken); } return result; }}

测试代码:

public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/data-source.xml"); RedisClientTemplate redisClient = (RedisClientTemplate)ac.getBean("redisClientTemplate"); redisClient.set("a", "abc"); System.out.println(redisClient.get("a")); }

附上RedisClientTemplate全部实现:

RedisClientTemplate代码太多,附上下载地址:http://xiazai.jb51.net/201701/yuanma/RedisClientTemplate_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章