一:如果你需要在你的本地项目中配置redis.那么你首先得需要在你的本地安装redis
参考链接【http://www.runoob.com/redis/redis-install.html】
然后 安装这个http://www.runoob.com/redis/redis-install.html ?
进行配置 当然很重要的一步是你需要在 redis.windows.conf 加入
两句 防止 内存问题 导致的启动不成功.
二:安装redis成功之后 .需要在项目中 进行redis配置
redis_ip=12⑦0.0.1
redis_port=6379
#当池内没有返回对象时,最大等待时间
redis_maxWaitMillis=10000
#当调用borrow Object方法时,是否进行有效性检查
redis_testOnBorrow=false
#当调用return Object方法时,是否进行有效性检查
redis_testOnReturn=false
redis_testWhileIdle = true
redis_maxTotal=100
#最大能够保持idel状态的对象数
redis_maxIdle=10
#是否开启缓存
enableCache=true
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w③org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis_ip}" /> <property name="port" value="${redis_port}" /> <property name="poolConfig" ref="jedisPoolConfig" /> bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory">property> bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxWaitMillis" value="${redis_maxWaitMillis}"/> <property name="testOnBorrow" value="${redis_testOnBorrow}"/> <property name="testOnReturn" value="${redis_testOnReturn}"/> <property name="testWhileIdle" value="${redis_testWhileIdle}"/> <property name="maxTotal" value="${redis_maxTotal}"/> <property name="maxIdle" value="${redis_maxIdle}"/> <property name="timeBetweenEvictionRunsMillis" value="30000"/> <property name="minEvictableIdleTimeMillis" value="30000"/> bean> <bean class="com.cdms.service.cache.impl.IRedisCacheServiceImpl"> <property name="redisTemplate" ref="redisTemplate">property> <property name="enableCache" value="${enableCache}">property> bean> beans>
对于在代码中的操作 只需要定义一个借口 和 一个实现类就行
package com.cdms.service.cache; import com.alibaba.fastjson.TypeReference; /** * 创建 by 草帽boy on 2017/3/31. */ public interface ICacheService{ /** * @param key 在缓存中的key值 * @param value 待储存的value值 * @param liveTime 存活时间 单位是秒 */ void set(final String key,final Object value,final long liveTime); /** * @param key 获取的key值 * @param type 获取的类型 * @param泛型 * @return 你所需要的类型值 */T get(final String key, final TypeReference type ); /** * @param key 返回 * @return */ long getLiveTime(final String key); /** * @param key key值 */ void del(final String key); }
package com.cdms.service.cache.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.cdms.service.cache.ICacheService; import com.cdms.util.SerializeUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.io.UnsupportedEncodingException; /** * 创建 by 草帽boy on 2017/3/31. */ public class IRedisCacheServiceImpl implements ICacheService{ public final Logger logger = LoggerFactory.getLogger(this.getClass()); //是否缓存.默认是不缓存 private static boolean enableCache = false; private static RedisTemplate redisTemplate; public boolean isEnableCache() { return enableCache; public void setEnableCache(boolean enableCache) { IRedisCacheServiceImpl.enableCache = enableCache; public RedisTemplate getRedisTemplate() { return redisTemplate; public void setRedisTemplate(RedisTemplate redisTemplate) { IRedisCacheServiceImpl.redisTemplate = redisTemplate; private void set(final byte[] key, final byte[] value, final long ttl) { if (enableCache == false) { return; redisTemplate.execute(new RedisCallback() { public Void doInRedis(RedisConnection con) { try { if (ttl != 0) con.expire(key, ttl); } catch (Exception ex) { return null; protected byte[] get(final byte[] key) { if (enableCache == false) { return null; try { return (byte[]) redisTemplate.execute(new RedisCallback<byte[]>() { public byte[] doInRedis(RedisConnection con) { return con.get(key); } catch (Exception ex) { return null; private long ttl(final byte[] key) { if (enableCache == false) { return 0; try { Long t = (Long) redisTemplate.execute(new RedisCallback () { public Long doInRedis(RedisConnection con) { return con.ttl(key); return t.longValue(); }catch (Exception ex){ return 0; protected void del(final byte[]... key) { if (enableCache == false || key == null) { return; try { redisTemplate.execute(new RedisCallback () { public Void doInRedis(RedisConnection con) { return null; } catch (Exception ex) { public void set(String key, Object value, long liveTime) { byte[] keys = key.getBytes(); String valString = JSON.toJSONString(value); byte[] values = valString.getBytes(); public T get(String key, TypeReference type) { byte[] valueData = get(key.getBytes()); if(valueData==null||valueData.length<0){ return null; String valString = ""; try { valString = new String(valueData,"UTF-8"); } catch (UnsupportedEncodingException e) { T data = JSON.parseObject(valString,type); return data; public long getLiveTime(String key) { return ttl(key.getBytes()); public void del(String key) { } }
三:测试 当你需要进行写入redis缓存的时候 你的redis服务器必须是开着的
就是 你的reids应该在这个状态:
@Test public void tests(){ Listmm = new ArrayList (); mm.add("土嘎嘎的粉丝们大家好啊"); mm.add("tests"); iCacheService.set("test",mm,60); List tesss = iCacheService.get("test",new TypeReference >(){}); if(tesss!=null){ System.out.println(">>>>>>>>>>>"+tesss); System.out.println(">>>>>>>>>>>>>"+iCacheService.getLiveTime("test")); }
结果:
以上就是土嘎嘎小编为大家整理的浅析redis缓存_在spring中的配置_及其简单的使用相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!