- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
- <!-- 连接池配置 -->
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <!-- 最大连接数 -->
- <property name="maxTotal" value="30" />
- <!-- 最大空闲连接数 -->
- <property name="maxIdle" value="10" />
- <!-- 每次释放连接的最大数目 -->
- <property name="numTestsPerEvictionRun" value="1024" />
- <!-- 释放连接的扫描间隔(毫秒) -->
- <property name="timeBetweenEvictionRunsMillis" value="30000" />
- <!-- 连接最小空闲时间 -->
- <property name="minEvictableIdleTimeMillis" value="1800000" />
- <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
- <property name="softMinEvictableIdleTimeMillis" value="10000" />
- <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
- <property name="maxWaitMillis" value="1500" />
- <!-- 在获取连接的时候检查有效性, 默认false -->
- <property name="testOnBorrow" value="true" />
- <!-- 在空闲时检查有效性, 默认false -->
- <property name="testWhileIdle" value="true" />
- <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
- <property name="blockWhenExhausted" value="false" />
- </bean>
- <!-- jedis客户端单机版 -->
- <bean id="redisClient" class="redis.clients.jedis.JedisPool">
- <constructor-arg name="host" value="192.168.116.13"></constructor-arg>
- <constructor-arg name="port" value="6379"></constructor-arg>
- <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
- </bean>
- <bean id="jedisClient" class="com.taotao.rest.daoImpl.JedisClientCluster"></bean>
- <!-- <bean id="redisClient" class="redis.clients.jedis.JedisCluster">
- <constructor-arg name="nodes">
- <set>
- <bean class="redis.clients.jedis.HostAndPort">
- <constructor-arg name="host" value="192.168.116.13"></constructor-arg>
- <constructor-arg name="port" value="7001"></constructor-arg>
- </bean>
- <bean class="redis.clients.jedis.HostAndPort">
- <constructor-arg name="host" value="192.168.116.13"></constructor-arg>
- <constructor-arg name="port" value="7002"></constructor-arg>
- </bean>
- <bean class="redis.clients.jedis.HostAndPort">
- <constructor-arg name="host" value="192.168.116.13"></constructor-arg>
- <constructor-arg name="port" value="7003"></constructor-arg>
- </bean>
- <bean class="redis.clients.jedis.HostAndPort">
- <constructor-arg name="host" value="192.168.116.13"></constructor-arg>
- <constructor-arg name="port" value="7004"></constructor-arg>
- </bean>
- <bean class="redis.clients.jedis.HostAndPort">
- <constructor-arg name="host" value="192.168.116.13"></constructor-arg>
- <constructor-arg name="port" value="7005"></constructor-arg>
- </bean>
- <bean class="redis.clients.jedis.HostAndPort">
- <constructor-arg name="host" value="192.168.116.13"></constructor-arg>
- <constructor-arg name="port" value="7006"></constructor-arg>
- </bean>
- </set>
- </constructor-arg>
- <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
- </bean>
- <bean id="JedisClientCluster" class="com.taotao.rest.daoImpl.JedisClientCluster"></bean>
- -->
-
- </beans>
单机版java
- package com.taotao.rest.daoImpl;
-
- import org.springframework.beans.factory.annotation.Autowired;
-
- import com.taotao.rest.dao.JedisClient;
-
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
-
- public class JedisClientSingle implements JedisClient{
-
- @Autowired
- private JedisPool jedisPool;
-
- @Override
- public String get(String key) {
- Jedis jedis = jedisPool.getResource();
- String string = jedis.get(key);
- jedis.close();
- return string;
- }
-
- @Override
- public String set(String key, String value) {
- Jedis jedis = jedisPool.getResource();
- String string = jedis.set(key, value);
- jedis.close();
- return string;
- }
-
- @Override
- public String hget(String hkey, String key) {
- Jedis jedis = jedisPool.getResource();
- String string = jedis.hget(hkey, key);
- jedis.close();
- return string;
- }
-
- @Override
- public long hset(String hkey, String key, String value) {
- Jedis jedis = jedisPool.getResource();
- Long result = jedis.hset(hkey, key, value);
- jedis.close();
- return result;
- }
-
- @Override
- public long incr(String key) {
- Jedis jedis = jedisPool.getResource();
- Long result = jedis.incr(key);
- jedis.close();
- return result;
- }
-
- @Override
- public long expire(String key, int second) {
- Jedis jedis = jedisPool.getResource();
- Long result = jedis.expire(key, second);
- jedis.close();
- return result;
- }
-
- @Override
- public long ttl(String key) {
- Jedis jedis = jedisPool.getResource();
- Long result = jedis.ttl(key);
- jedis.close();
- return result;
- }
-
- }
集群版的
- package com.taotao.rest.daoImpl;
-
- import org.springframework.beans.factory.annotation.Autowired;
-
- import redis.clients.jedis.JedisCluster;
-
- import com.taotao.rest.dao.JedisClient;
-
- public class JedisClientCluster implements JedisClient {
-
- @Autowired
- private JedisCluster jedisCluster;
-
- @Override
- public String get(String key) {
- return jedisCluster.get(key);
- }
-
- @Override
- public String set(String key, String value) {
- return jedisCluster.set(key, value);
- }
- //哈希的get
- @Override
- public String hget(String hkey, String key) {
- return jedisCluster.hget(hkey, key);
- }
- //哈希的set
- @Override
- public long hset(String hkey, String key, String value) {
- return jedisCluster.hset(hkey, key, value);
- }
- //设置缓存对象值+1
- @Override
- public long incr(String key) {
- return jedisCluster.incr(key);
- }
- //设置缓存对象超时时间
- @Override
- public long expire(String key, int second) {
- return jedisCluster.expire(key, second);
- }
- //判断缓存对象是否存在
- //TTL以毫秒为单位。
-
- //-1, 如果key没有到期超时。
-
- //-2, 如果键不存在。
- @Override
- public long ttl(String key) {
- return jedisCluster.ttl(key);
- }
-
- }
使用用例
配置文件
名字:resource.properties
内容
INDEX_CONTENT_REDIS_KEY=INDEX_CONTENT_REDIS_KEY
- package com.taotao.rest.service.impl;
-
- import java.util.List;
-
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
-
- import com.taotao.common.utils.JsonUtils;
- import com.taotao.mapper.TbContentMapper;
- import com.taotao.pojo.TbContent;
- import com.taotao.pojo.TbContentExample;
- import com.taotao.pojo.TbContentExample.Criteria;
- import com.taotao.rest.dao.JedisClient;
- import com.taotao.rest.service.ContentService;
-
- @Service
- public class ContentServiceImpl implements ContentService{
-
- @Autowired
- private TbContentMapper contentMapper;
- @Autowired
- private JedisClient jedisClient;
- @Value("${INDEX_CONTENT_REDIS_KEY}")
- private String INDEX_CONTENT_REDIS_KEY;
- @Override
- public List<TbContent> getContentList(long contentCid) {
- //从缓存中取出
- try {
- String result = jedisClient.hget(INDEX_CONTENT_REDIS_KEY, contentCid+"");
- if(!StringUtils.isBlank(result)){
- List<TbContent> resultlist = JsonUtils.jsonToList(result, TbContent.class);
- return resultlist;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- TbContentExample example = new TbContentExample();
- Criteria criteria = example.createCriteria();
- criteria.andCategoryIdEqualTo(contentCid);
-
- List<TbContent> list = contentMapper.selectByExample(example);
-
- //像缓存中添加内容
- try {
- //把list转换成字符串
- String cacheString =JsonUtils.objectToJson(list);
- jedisClient.hset(INDEX_CONTENT_REDIS_KEY, contentCid+"", cacheString);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return list;
- }
-
- }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。