文档章节

Spring cache Redis Key生成策略

lis1314
 lis1314
发布于 2016/07/08 20:56
字数 5616
阅读 4086
收藏 8

最近使用Spring cache,发现使用默认生成的key策略只使用了方法入参作为key,很不灵活,用到真实的项目中也不太靠谱,于是自己实现它的key生成策略。

参考官方文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html

spring 默认的key生成实现是

org.springframework.cache.interceptor.SimpleKeyGenerator

感兴趣的同学自己看下,具体我就不描述了。

自定义生成策略需要实现下面的接口: 接口对应方法参数,需要调用的目标对象实例,目标方法,目标方法入参

public interface KeyGenerator {

	/**
	 * Generate a key for the given method and its parameters.
	 * @param target the target instance
	 * @param method the method being called
	 * @param params the method parameters (with any var-args expanded)
	 * @return a generated key
	 */
	Object generate(Object target, Method method, Object... params);

}

下面展示我实现的方式

import java.io.Serializable;
import java.lang.reflect.Method;

import org.springframework.cache.interceptor.KeyGenerator;

/**
 * 实现spring cache的默认缓存实现策略
 * @author lisuo
 *
 */
public class DefaultKeyGenerator implements Serializable,KeyGenerator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5944569667769217577L;

	@Override
	public Object generate(Object target, Method method, Object... params) {
		return new DefaultKey(target, method, params);
	}
}

这里必须要知道的是,spring cache在判断是否命中数据时,是基于底层的缓存实现判断是否命中key的。

下面结合redis展示我的实现:

import java.io.Serializable;
import java.lang.reflect.Method;

import org.apache.commons.lang3.ArrayUtils;


/**
 * Spring cache key 生成策略
 * @author lisuo
 */
public class DefaultKey implements Serializable{
	
	private static final long serialVersionUID = -8536541050699277557L;
	
	/** 调用目标对象全类名 */
	protected String targetClassName;
	/** 调用目标方法名称 */
	protected String methodName;
	/** 调用目标参数 */
	protected Object[] params;
	
	public DefaultKey(Object target, Method method, Object[] elements) {
		this.targetClassName = target.getClass().getName();
		this.methodName = generatorMethodName(method);
		if(ArrayUtils.isNotEmpty(elements)){
			this.params = new Object[elements.length];
			for(int i =0;i<elements.length;i++){
				Object ele = elements[i];
				this.params[i] = ele;
			}
		}
	}
	
	private String generatorMethodName(Method method){
		StringBuilder builder = new StringBuilder(method.getName());
		Class<?>[] types = method.getParameterTypes();
		if(ArrayUtils.isNotEmpty(types)){
			builder.append("(");
			for(Class<?> type:types){
				String name = type.getName();
				builder.append(name+",");
			}
			builder.delete(builder.length()-1, builder.length());
			builder.append(")");
		}
		return builder.toString();
	}
	
}

上面只是缓存应用的实现,具体我们看下Redis底层缓存管理器的实现

首先是CacheManager

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.support.AbstractCacheManager;
import org.springframework.util.Assert;

import com.study.demo.util.RedisUtil;

/**
 * Redis缓存管理器,实现Spring Cache接口
 * @author lisuo
 *
 */
public class RedisCacheManager extends AbstractCacheManager {
	
	private RedisUtil rcache;
	
	//缓存前缀
	private String cachePrefix;
	
	//储存缓存名称的set集合key名称
	private String cacheNameSet;
	
	public RedisCacheManager(RedisUtil rcache,String cachePrefix,String cacheNameSet) {
		Assert.notNull(rcache, "Cache must not be null");
		Assert.notNull(cachePrefix, "CachePrefix must not be null");
		Assert.notNull(cacheNameSet, "CacheNameSet must not be null");
		this.rcache = rcache;
		this.cachePrefix = cachePrefix;
		this.cacheNameSet = cacheNameSet;
	}
	
	@Override
	protected Collection<? extends Cache> loadCaches() {
		List<Cache> caches = new ArrayList<>();
		Set<String> names = rcache.smembers(cacheNameSet);
		if(CollectionUtils.isNotEmpty(names)){
			for (String name:names) {
				RedisCache cache = new RedisCache(cachePrefix+name, rcache);
				caches.add(cache);
			}
		}
		return caches;
	}
	
	
	@Override
	protected Cache getMissingCache(String name) {
		String  cacheName = cachePrefix+name;
		rcache.sadd(cacheNameSet, cacheName);
		RedisCache cache = new RedisCache(cacheName, rcache);
		return cache;
	}
	
}

Cache的实现

import java.util.concurrent.Callable;

import org.springframework.cache.support.AbstractValueAdaptingCache;
import org.springframework.util.Assert;

import com.study.demo.util.RedisUtil;

/**
 * 缓存实现
 * 这个结构的缓存是不支持数据过期的
 * @author lisuo
 *
 */
public class RedisCache extends AbstractValueAdaptingCache{
	
	private final String name;
	
	private RedisUtil rcache;
	
	public RedisCache(String name,RedisUtil rcache) {
		super(true);
		Assert.notNull(name, "Name must not be null");
		Assert.notNull(rcache, "Cache must not be null");
		this.name = name;
		this.rcache = rcache;
	}

	@Override
	public final String getName() {
		return this.name;
	}

	@Override
	public Object getNativeCache() {
		return this.rcache;
	}

	@Override
	public ValueWrapper get(Object key) {
		String v = rcache.hget(name, ObjStringConverUtil.objToString(key));
		if(v!=null){
			Object value = ObjStringConverUtil.stringToObj(v);
			ValueWrapper valueWrapper = toValueWrapper(value);
			return valueWrapper;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T get(Object key, Callable<T> valueLoader) {
		Object v = this.lookup(key);
		if(v!=null){
			return (T) v;
		}
		else {
			T value;
			try {
				value = valueLoader.call();
			}
			catch (Exception ex) {
				throw new ValueRetrievalException(key, valueLoader, ex);
			}
			put(key, value);
			return value;
		}
		
	}

	@Override
	public void put(Object key, Object value) {
		rcache.hset(name, ObjStringConverUtil.objToString(key), ObjStringConverUtil.objToString(value));
	}

	@Override
	public ValueWrapper putIfAbsent(Object key, Object value) {
		String k = ObjStringConverUtil.objToString(key);
		Boolean hexists = rcache.hexists(name, k);
		if(hexists){
			String v = rcache.hget(name, k);
			if(v!=null){
				ValueWrapper valueWrapper = toValueWrapper(ObjStringConverUtil.stringToObj(v));
				return valueWrapper;
			}
		}
		String val = ObjStringConverUtil.objToString(value);
		rcache.hset(name, k, val);
		return toValueWrapper(value);
	}

	@Override
	public void evict(Object key) {
		String k = ObjStringConverUtil.objToString(key);
		rcache.hdel(name, k);
	}

	@Override
	public void clear() {
		rcache.del(name);
	}

	@Override
	protected Object lookup(Object key) {
		String v = rcache.hget(name, ObjStringConverUtil.objToString(key));
		if(v!=null){
			Object value = ObjStringConverUtil.stringToObj(v);
			return value;
		}
		return null;
	}

}

对象转换字符工具类

import java.io.Serializable;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.SerializationUtils;

/**
 * 对象字符串互转工具类
 * 序列化+Base64实现
 * @author lisuo
 *
 */
public class ObjStringConverUtil {
	
	/**
	 * 对象转字符串
	 * @param obj
	 * @return
	 */
	public static String objToString(Object obj){
		byte[] bs = SerializationUtils.serialize((Serializable) obj);
		return Base64.encodeBase64String(bs);
	}
	
	/**
	 * 字符串转对象
	 * @param str
	 * @return
	 */
	public static Object stringToObj(String str){
		byte[] bs = Base64.decodeBase64(str);
		Object obj = SerializationUtils.deserialize(bs);
		return obj;
	}
}

Redis操作工具类

关于Redis的工具类代码比较多

具体使用介绍参看:https://my.oschina.net/lis1314/blog/881704

package com.study.demo.util;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.BitPosParams;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.params.geo.GeoRadiusParam;
import redis.clients.jedis.params.sortedset.ZAddParams;
import redis.clients.jedis.params.sortedset.ZIncrByParams;

/**
 * Jedis 操作工具类
 * 
 * @version 基于Jedis 2.9.0
 * @author lisuo
 *
 */
public class RedisUtil implements JedisCommands {

	// jedis连接池
	private JedisPool jedisPool;

	// jedis集群
	private JedisCluster jedisCluster;

	// 是否为集群,默认不是集群
	private boolean isCluster = false;

	/**
	 * 连接池构建方式
	 * @param jedisPool  jedis连接池
	 */
	public RedisUtil(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	/**
	 * 集群构建方式
	 * @param jedisCluster jedis集群实例
	 */
	public RedisUtil(JedisCluster jedisCluster) {
		this.jedisCluster = jedisCluster;
		isCluster = true;
	}

	// -----------------------------------实现脚本命令-----------------------------------
	public Object eval(String script, int keyCount, String... params) {
		if (isCluster) {
			return jedisCluster.eval(script, keyCount, params);
		} else {
			Jedis jedis = jedisPool.getResource();
			try {
				return jedis.eval(script, keyCount, params);
			} finally {
				jedis.close();
			}
		}
	}

	public Object eval(String script, List<String> keys, List<String> args) {
		if (isCluster) {
			return jedisCluster.eval(script, keys, args);
		} else {
			Jedis jedis = jedisPool.getResource();
			try {
				return jedis.eval(script, keys, args);
			} finally {
				jedis.close();
			}
		}
	}

	public Object evalsha(String sha1, int keyCount, String... params) {
		if (isCluster) {
			return jedisCluster.evalsha(sha1, keyCount, params);
		} else {
			Jedis jedis = jedisPool.getResource();
			try {
				return jedis.evalsha(sha1, keyCount, params);
			} finally {
				jedis.close();
			}
		}
	}

	/**
	 * 获取JedisCommands实例
	 * @return JedisCommands
	 */
	private JedisCommands getJedisCommands() {
		if (isCluster) {
			return jedisCluster;
		} else {
			return jedisPool.getResource();
		}
	}

	/**
	 * Callback 回调接口
	 * @param <T>
	 */
	public interface Callback<T> {
		/**
		 * 回调函数
		 * @param commands
		 * @return
		 */
		public T call(JedisCommands commands);
	}

	/**
	 * 执行Redis 命令
	 * @param callback 回调接口
	 * @return
	 */
	public <T> T execute(Callback<T> callback) {
		JedisCommands jedisCommands = getJedisCommands();
		try {
			return callback.call(jedisCommands);
		} finally {
			// 非集群下释放资源,集群源码中已实现释放资源
			if (!isCluster) {
				((Jedis) jedisCommands).close();
			}
		}
	}

	// 实现JedisCommands,关于@Deprecated标记的方法参看Jedis API,如果报错可能是版本过高,一些方法被废除
	@Override
	public String get(final String key) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.get(key);
			}
		});
	}

	@Override
	public String set(final String key, final String value) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.set(key, value);
			}
		});
	}

	@Override
	public Long sadd(final String key, final String... member) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.sadd(key, member);
			}
		});
	}

	@Override
	public Long srem(final String key, final String... member) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.srem(key, member);
			}
		});
	}

	@Override
	public Boolean sismember(final String key, final String member) {
		return execute(new Callback<Boolean>() {
			@Override
			public Boolean call(JedisCommands commands) {
				return commands.sismember(key, member);
			}
		});
	}

	@Override
	public String hget(final String key, final String field) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.hget(key, field);
			}
		});
	}

	@Override
	public String hmset(final String key, final Map<String, String> hash) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.hmset(key, hash);
			}
		});
	}

	@Override
	public Long hset(final String key, final String field, final String value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.hset(key, field, value);
			}
		});
	}

	@Override
	public Long lpush(final String key, final String... string) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.lpush(key, string);
			}
		});
	}

	@Override
	public String lpop(final String key) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.lpop(key);
			}
		});
	}

	@Override
	public String set(final String key, final String value, final String nxxx, final String expx, final long time) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.set(key, value, nxxx, expx, time);
			}
		});
	}

	@Override
	public String set(final String key, final String value, final String nxxx) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.set(key, value, nxxx);
			}
		});
	}

	@Override
	public Boolean exists(final String key) {
		return execute(new Callback<Boolean>() {
			@Override
			public Boolean call(JedisCommands commands) {
				return commands.exists(key);
			}
		});
	}

	@Override
	public Long persist(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.persist(key);
			}
		});
	}

	@Override
	public String type(final String key) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.type(key);
			}
		});
	}

	@Override
	public Long expire(final String key, final int seconds) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.expire(key, seconds);
			}
		});
	}

	@Override
	public Long pexpire(final String key, final long milliseconds) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.pexpire(key, milliseconds);
			}
		});
	}

	@Override
	public Long expireAt(final String key, final long unixTime) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.expireAt(key, unixTime);
			}
		});
	}

	@Override
	public Long pexpireAt(final String key, final long millisecondsTimestamp) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.pexpireAt(key, millisecondsTimestamp);
			}
		});
	}

	@Override
	public Long ttl(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.ttl(key);
			}
		});
	}

	@Override
	public Long pttl(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.pttl(key);
			}
		});
	}

	@Override
	public Boolean setbit(final String key, final long offset, final boolean value) {
		return execute(new Callback<Boolean>() {
			@Override
			public Boolean call(JedisCommands commands) {
				return commands.setbit(key, offset, value);
			}
		});
	}

	@Override
	public Boolean setbit(final String key, final long offset, final String value) {
		return execute(new Callback<Boolean>() {
			@Override
			public Boolean call(JedisCommands commands) {
				return commands.setbit(key, offset, value);
			}
		});
	}

	@Override
	public Boolean getbit(final String key, final long offset) {
		return execute(new Callback<Boolean>() {
			@Override
			public Boolean call(JedisCommands commands) {
				return commands.getbit(key, offset);
			}
		});
	}

	@Override
	public Long setrange(final String key, final long offset, final String value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.setrange(key, offset, value);
			}
		});
	}

	@Override
	public String getrange(final String key, final long startOffset, final long endOffset) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.getrange(key, startOffset, endOffset);
			}
		});
	}

	@Override
	public String getSet(final String key, final String value) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.getSet(key, value);
			}
		});
	}

	@Override
	public Long setnx(final String key, final String value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.setnx(key, value);
			}
		});
	}

	@Override
	public String setex(final String key, final int seconds, final String value) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.setex(key, seconds, value);
			}
		});
	}

	@Override
	public String psetex(final String key, final long milliseconds, final String value) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.psetex(key, milliseconds, value);
			}
		});
	}

	@Override
	public Long decrBy(final String key, final long integer) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.decrBy(key, integer);
			}
		});
	}

	@Override
	public Long decr(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.decr(key);
			}
		});
	}

	@Override
	public Long incrBy(final String key, final long integer) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.incrBy(key, integer);
			}
		});
	}

	@Override
	public Double incrByFloat(final String key, final double value) {
		return execute(new Callback<Double>() {
			@Override
			public Double call(JedisCommands commands) {
				return commands.incrByFloat(key, value);
			}
		});
	}

	@Override
	public Long incr(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.incr(key);
			}
		});
	}

	@Override
	public Long append(final String key, final String value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.append(key, value);
			}
		});
	}

	@Override
	public String substr(final String key, final int start, final int end) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.substr(key, start, end);
			}
		});
	}

	@Override
	public Long hsetnx(final String key, final String field, final String value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.hsetnx(key, field, value);
			}
		});
	}

	@Override
	public List<String> hmget(final String key, final String... fields) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.hmget(key, fields);
			}
		});
	}

	@Override
	public Long hincrBy(final String key, final String field, final long value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.hincrBy(key, field, value);
			}
		});
	}

	@Override
	public Double hincrByFloat(final String key, final String field, final double value) {
		return execute(new Callback<Double>() {
			@Override
			public Double call(JedisCommands commands) {
				return commands.hincrByFloat(key, field, value);
			}
		});
	}

	@Override
	public Boolean hexists(final String key, final String field) {
		return execute(new Callback<Boolean>() {
			@Override
			public Boolean call(JedisCommands commands) {
				return commands.hexists(key, field);
			}
		});
	}

	@Override
	public Long hdel(final String key, final String... field) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.hdel(key, field);
			}
		});
	}

	@Override
	public Long hlen(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.hlen(key);
			}
		});
	}

	@Override
	public Set<String> hkeys(final String key) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.hkeys(key);
			}
		});
	}

	@Override
	public List<String> hvals(final String key) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.hvals(key);
			}
		});
	}

	@Override
	public Map<String, String> hgetAll(final String key) {
		return execute(new Callback<Map<String, String>>() {
			@Override
			public Map<String, String> call(JedisCommands commands) {
				return commands.hgetAll(key);
			}
		});
	}

	@Override
	public Long rpush(final String key, final String... string) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.rpush(key, string);
			}
		});
	}

	@Override
	public Long llen(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.llen(key);
			}
		});
	}

	@Override
	public List<String> lrange(final String key, final long start, final long end) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.lrange(key, start, end);
			}
		});
	}

	@Override
	public String ltrim(final String key, final long start, final long end) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.ltrim(key, start, end);
			}
		});
	}

	@Override
	public String lindex(final String key, final long index) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.lindex(key, index);
			}
		});
	}

	@Override
	public String lset(final String key, final long index, final String value) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.lset(key, index, value);
			}
		});
	}

	@Override
	public Long lrem(final String key, final long count, final String value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.lrem(key, count, value);
			}
		});
	}

	@Override
	public String rpop(final String key) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.rpop(key);
			}
		});
	}

	@Override
	public Set<String> smembers(final String key) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.smembers(key);
			}
		});
	}

	@Override
	public String spop(final String key) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.spop(key);
			}
		});
	}

	@Override
	public Set<String> spop(final String key, final long count) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.spop(key, count);
			}
		});
	}

	@Override
	public Long scard(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.scard(key);
			}
		});
	}

	@Override
	public String srandmember(final String key) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.srandmember(key);
			}
		});
	}

	@Override
	public List<String> srandmember(final String key, final int count) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.srandmember(key, count);
			}
		});
	}

	@Override
	public Long strlen(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.strlen(key);
			}
		});
	}

	@Override
	public Long zadd(final String key, final double score, final String member) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zadd(key, score, member);
			}
		});
	}

	@Override
	public Long zadd(final String key, final double score, final String member, final ZAddParams params) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zadd(key, score, member, params);
			}
		});
	}

	@Override
	public Long zadd(final String key, final Map<String, Double> scoreMembers) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zadd(key, scoreMembers);
			}
		});
	}

	@Override
	public Long zadd(final String key, final Map<String, Double> scoreMembers, final ZAddParams params) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zadd(key, scoreMembers, params);
			}
		});
	}

	@Override
	public Set<String> zrange(final String key, final long start, final long end) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrange(key, start, end);
			}
		});
	}

	@Override
	public Long zrem(final String key, final String... member) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zrem(key, member);
			}
		});
	}

	@Override
	public Double zincrby(final String key, final double score, final String member) {
		return execute(new Callback<Double>() {
			@Override
			public Double call(JedisCommands commands) {
				return commands.zincrby(key, score, member);
			}
		});
	}

	@Override
	public Double zincrby(final String key, final double score, final String member, final ZIncrByParams params) {
		return execute(new Callback<Double>() {
			@Override
			public Double call(JedisCommands commands) {
				return commands.zincrby(key, score, member, params);
			}
		});
	}

	@Override
	public Long zrank(final String key, final String member) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zrank(key, member);
			}
		});
	}

	@Override
	public Long zrevrank(final String key, final String member) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zrevrank(key, member);
			}
		});
	}

	@Override
	public Set<String> zrevrange(final String key, final long start, final long end) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrevrange(key, start, end);
			}
		});
	}

	@Override
	public Set<Tuple> zrangeWithScores(final String key, final long start, final long end) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrangeWithScores(key, start, end);
			}
		});
	}

	@Override
	public Set<Tuple> zrevrangeWithScores(final String key, final long start, final long end) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrevrangeWithScores(key, start, end);
			}
		});
	}

	@Override
	public Long zcard(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zcard(key);
			}
		});
	}

	@Override
	public Double zscore(final String key, final String member) {
		return execute(new Callback<Double>() {
			@Override
			public Double call(JedisCommands commands) {
				return commands.zscore(key, member);
			}
		});
	}

	@Override
	public List<String> sort(final String key) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.sort(key);
			}
		});
	}

	@Override
	public List<String> sort(final String key, final SortingParams sortingParameters) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.sort(key, sortingParameters);
			}
		});
	}

	@Override
	public Long zcount(final String key, final double min, final double max) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zcount(key, min, max);
			}
		});
	}

	@Override
	public Long zcount(final String key, final String min, final String max) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zcount(key, min, max);
			}
		});
	}

	@Override
	public Set<String> zrangeByScore(final String key, final double min, final double max) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrangeByScore(key, min, max);
			}
		});
	}

	@Override
	public Set<String> zrangeByScore(final String key, final String min, final String max) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrangeByScore(key, min, max);
			}
		});
	}

	@Override
	public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrevrangeByScore(key, max, min);
			}
		});
	}

	@Override
	public Set<String> zrangeByScore(final String key, final double min, final double max, final int offset,
			final int count) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrangeByScore(key, min, max, offset, count);
			}
		});
	}

	@Override
	public Set<String> zrevrangeByScore(final String key, final String max, final String min) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrevrangeByScore(key, max, min);
			}
		});
	}

	@Override
	public Set<String> zrangeByScore(final String key, final String min, final String max, final int offset,
			final int count) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrangeByScore(key, min, max, offset, count);
			}
		});
	}

	@Override
	public Set<String> zrevrangeByScore(final String key, final double max, final double min, final int offset,
			final int count) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrevrangeByScore(key, max, min, offset, count);
			}
		});
	}

	@Override
	public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrangeByScoreWithScores(key, min, max);
			}
		});
	}

	@Override
	public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrevrangeByScoreWithScores(key, max, min);
			}
		});
	}

	@Override
	public Set<Tuple> zrangeByScoreWithScores(final String key, final double min, final double max, final int offset,
			final int count) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrangeByScoreWithScores(key, min, max, offset, count);
			}
		});
	}

	@Override
	public Set<String> zrevrangeByScore(final String key, final String max, final String min, final int offset,
			final int count) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrevrangeByScore(key, max, min, offset, count);
			}
		});
	}

	@Override
	public Set<Tuple> zrangeByScoreWithScores(final String key, final String min, final String max) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrangeByScoreWithScores(key, min, max);
			}
		});
	}

	@Override
	public Set<Tuple> zrevrangeByScoreWithScores(final String key, final String max, final String min) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrevrangeByScoreWithScores(key, max, min);
			}
		});
	}

	@Override
	public Set<Tuple> zrangeByScoreWithScores(final String key, final String min, final String max, final int offset,
			final int count) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrangeByScoreWithScores(key, min, max, offset, count);
			}
		});
	}

	@Override
	public Set<Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min, final int offset,
			final int count) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrevrangeByScoreWithScores(key, max, min, offset, count);
			}
		});
	}

	@Override
	public Set<Tuple> zrevrangeByScoreWithScores(final String key, final String max, final String min, final int offset,
			final int count) {
		return execute(new Callback<Set<Tuple>>() {
			@Override
			public Set<Tuple> call(JedisCommands commands) {
				return commands.zrevrangeByScoreWithScores(key, max, min, offset, count);
			}
		});
	}

	@Override
	public Long zremrangeByRank(final String key, final long start, final long end) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zremrangeByRank(key, start, end);
			}
		});
	}

	@Override
	public Long zremrangeByScore(final String key, final double start, final double end) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zremrangeByScore(key, start, end);
			}
		});
	}

	@Override
	public Long zremrangeByScore(final String key, final String start, final String end) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zremrangeByScore(key, start, end);
			}
		});
	}

	@Override
	public Long zlexcount(final String key, final String min, final String max) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zlexcount(key, min, max);
			}
		});
	}

	@Override
	public Set<String> zrangeByLex(final String key, final String min, final String max) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrangeByLex(key, min, max);
			}
		});
	}

	@Override
	public Set<String> zrangeByLex(final String key, final String min, final String max, final int offset,
			final int count) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrangeByLex(key, min, max, offset, count);
			}
		});
	}

	@Override
	public Set<String> zrevrangeByLex(final String key, final String max, final String min) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrevrangeByLex(key, max, min);
			}
		});
	}

	@Override
	public Set<String> zrevrangeByLex(final String key, final String max, final String min, final int offset,
			final int count) {
		return execute(new Callback<Set<String>>() {
			@Override
			public Set<String> call(JedisCommands commands) {
				return commands.zrevrangeByLex(key, max, min, offset, count);
			}
		});
	}

	@Override
	public Long zremrangeByLex(final String key, final String min, final String max) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.zremrangeByLex(key, min, max);
			}
		});
	}

	@Override
	public Long linsert(final String key, final LIST_POSITION where, final String pivot, final String value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.linsert(key, where, pivot, value);
			}
		});
	}

	@Override
	public Long lpushx(final String key, final String... string) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.lpushx(key, string);
			}
		});
	}

	@Override
	public Long rpushx(final String key, final String... string) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.rpushx(key, string);
			}
		});
	}

	@Deprecated
	@Override
	public List<String> blpop(final String arg) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.blpop(arg);
			}
		});
	}

	@Override
	public List<String> blpop(final int timeout, final String key) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.blpop(timeout, key);
			}
		});
	}

	@Deprecated
	@Override
	public List<String> brpop(final String arg) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.brpop(arg);
			}
		});
	}

	@Override
	public List<String> brpop(final int timeout, final String key) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.brpop(timeout, key);
			}
		});
	}

	@Override
	public Long del(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.del(key);
			}
		});
	}

	@Override
	public String echo(final String string) {
		return execute(new Callback<String>() {
			@Override
			public String call(JedisCommands commands) {
				return commands.echo(string);
			}
		});
	}

	@Override
	public Long move(final String key, final int dbIndex) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.move(key, dbIndex);
			}
		});
	}

	@Override
	public Long bitcount(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.bitcount(key);
			}
		});
	}

	@Override
	public Long bitcount(final String key, final long start, final long end) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.bitcount(key, start, end);
			}
		});
	}

	@Override
	public Long bitpos(final String key, final boolean value) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.bitpos(key, value);
			}
		});
	}

	@Override
	public Long bitpos(final String key, final boolean value, final BitPosParams params) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.bitpos(key, value, params);
			}
		});
	}

	@Deprecated
	@Override
	public ScanResult<Entry<String, String>> hscan(final String key, final int cursor) {
		return execute(new Callback<ScanResult<Entry<String, String>>>() {
			@Override
			public ScanResult<Entry<String, String>> call(JedisCommands commands) {
				return commands.hscan(key, cursor);
			}
		});
	}

	@Deprecated
	@Override
	public ScanResult<String> sscan(final String key, final int cursor) {
		return execute(new Callback<ScanResult<String>>() {
			@Override
			public ScanResult<String> call(JedisCommands commands) {
				return commands.sscan(key, cursor);
			}
		});
	}

	@Deprecated
	@Override
	public ScanResult<Tuple> zscan(final String key, final int cursor) {
		return execute(new Callback<ScanResult<Tuple>>() {
			@Override
			public ScanResult<Tuple> call(JedisCommands commands) {
				return commands.zscan(key, cursor);
			}
		});
	}

	@Override
	public ScanResult<Entry<String, String>> hscan(final String key, final String cursor) {
		return execute(new Callback<ScanResult<Entry<String, String>>>() {
			@Override
			public ScanResult<Entry<String, String>> call(JedisCommands commands) {
				return commands.hscan(key, cursor);
			}
		});
	}

	@Override
	public ScanResult<Entry<String, String>> hscan(final String key, final String cursor, final ScanParams params) {
		return execute(new Callback<ScanResult<Entry<String, String>>>() {
			@Override
			public ScanResult<Entry<String, String>> call(JedisCommands commands) {
				return commands.hscan(key, cursor, params);
			}
		});
	}

	@Override
	public ScanResult<String> sscan(final String key, final String cursor) {
		return execute(new Callback<ScanResult<String>>() {
			@Override
			public ScanResult<String> call(JedisCommands commands) {
				return commands.sscan(key, cursor);
			}
		});
	}

	@Override
	public ScanResult<String> sscan(final String key, final String cursor, final ScanParams params) {
		return execute(new Callback<ScanResult<String>>() {
			@Override
			public ScanResult<String> call(JedisCommands commands) {
				return commands.sscan(key, cursor, params);
			}
		});
	}

	@Override
	public ScanResult<Tuple> zscan(final String key, final String cursor) {
		return execute(new Callback<ScanResult<Tuple>>() {
			@Override
			public ScanResult<Tuple> call(JedisCommands commands) {
				return commands.zscan(key, cursor);
			}
		});
	}

	@Override
	public ScanResult<Tuple> zscan(final String key, final String cursor, final ScanParams params) {
		return execute(new Callback<ScanResult<Tuple>>() {
			@Override
			public ScanResult<Tuple> call(JedisCommands commands) {
				return commands.zscan(key, cursor, params);
			}
		});
	}

	@Override
	public Long pfadd(final String key, final String... elements) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.pfadd(key, elements);
			}
		});
	}

	@Override
	public long pfcount(final String key) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.pfcount(key);
			}
		});
	}

	@Override
	public Long geoadd(final String key, final double longitude, final double latitude, final String member) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.geoadd(key, longitude, latitude, member);
			}
		});
	}

	@Override
	public Long geoadd(final String key, final Map<String, GeoCoordinate> memberCoordinateMap) {
		return execute(new Callback<Long>() {
			@Override
			public Long call(JedisCommands commands) {
				return commands.geoadd(key, memberCoordinateMap);
			}
		});
	}

	@Override
	public Double geodist(final String key, final String member1, final String member2) {
		return execute(new Callback<Double>() {
			@Override
			public Double call(JedisCommands commands) {
				return commands.geodist(key, member1, member2);
			}
		});
	}

	@Override
	public Double geodist(final String key, final String member1, final String member2, final GeoUnit unit) {
		return execute(new Callback<Double>() {
			@Override
			public Double call(JedisCommands commands) {
				return commands.geodist(key, member1, member2, unit);
			}
		});
	}

	@Override
	public List<String> geohash(final String key, final String... members) {
		return execute(new Callback<List<String>>() {
			@Override
			public List<String> call(JedisCommands commands) {
				return commands.geohash(key, members);
			}
		});
	}

	@Override
	public List<GeoCoordinate> geopos(final String key, final String... members) {
		return execute(new Callback<List<GeoCoordinate>>() {
			@Override
			public List<GeoCoordinate> call(JedisCommands commands) {
				return commands.geopos(key, members);
			}
		});
	}

	@Override
	public List<GeoRadiusResponse> georadius(final String key, final double longitude, final double latitude,
			final double radius, final GeoUnit unit) {
		return execute(new Callback<List<GeoRadiusResponse>>() {
			@Override
			public List<GeoRadiusResponse> call(JedisCommands commands) {
				return commands.georadius(key, longitude, latitude, radius, unit);
			}
		});
	}

	@Override
	public List<GeoRadiusResponse> georadius(final String key, final double longitude, final double latitude,
			final double radius, final GeoUnit unit, final GeoRadiusParam param) {
		return execute(new Callback<List<GeoRadiusResponse>>() {
			@Override
			public List<GeoRadiusResponse> call(JedisCommands commands) {
				return commands.georadius(key, longitude, latitude, radius, unit, param);
			}
		});
	}

	@Override
	public List<GeoRadiusResponse> georadiusByMember(final String key, final String member, final double radius,
			final GeoUnit unit) {
		return execute(new Callback<List<GeoRadiusResponse>>() {
			@Override
			public List<GeoRadiusResponse> call(JedisCommands commands) {
				return commands.georadiusByMember(key, member, radius, unit);
			}
		});
	}

	@Override
	public List<GeoRadiusResponse> georadiusByMember(final String key, final String member, final double radius,
			final GeoUnit unit, final GeoRadiusParam param) {
		return execute(new Callback<List<GeoRadiusResponse>>() {
			@Override
			public List<GeoRadiusResponse> call(JedisCommands commands) {
				return commands.georadiusByMember(key, member, radius, unit, param);
			}
		});
	}

	@Override
	public List<Long> bitfield(final String key, final String... arguments) {
		return execute(new Callback<List<Long>>() {
			@Override
			public List<Long> call(JedisCommands commands) {
				return commands.bitfield(key, arguments);
			}
		});
	}

}

整合配置:Redis配置

package com.study.demo.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.study.demo.util.RedisUtil;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis 配置(单点)
 * @author lisuo
 *
 */
@Configuration
public class RedisConfiguration {

	@Bean(name = "redisPool")
	public JedisPool jedisPool(@Autowired @Qualifier("redisPoolConfig") JedisPoolConfig config,
			@Value("${redis.host1}") String host, @Value("${redis.port}") int port) {
		return new JedisPool(config, host, port);
	}

	@Bean(name = "redisPoolConfig")
	public JedisPoolConfig jedisPoolConfig(@Value("${redis.pool.maxIdle}") int maxTotal,
			@Value("${redis.pool.maxIdle}") int maxIdle, @Value("${redis.pool.maxWaitMillis}") int maxWaitMillis) {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(maxTotal);
		config.setMaxIdle(maxIdle);
		config.setMaxWaitMillis(maxWaitMillis);
		return config;
	}

	@Bean(name = "redisUtil")
	public RedisUtil redisUtil(@Autowired @Qualifier("redisPool") JedisPool jedisPool) {
		RedisUtil config = new RedisUtil(jedisPool);
		return config;
	}

}

缓存整合配置

package com.study.demo.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;

import com.study.demo.cache.DefaultKeyGenerator;
import com.study.demo.cache.RedisCacheManager;
import com.study.demo.util.RedisUtil;

@Configuration
@EnableCaching
public class SpringCacheConfiguraction extends CachingConfigurerSupport{
	
	@Autowired
	private RedisUtil redisUtil;
	
	@Override
	public CacheManager cacheManager() {
		RedisCacheManager manager = new RedisCacheManager(redisUtil, "study:cache:", "study:cacheNameSet");
		return manager;
	}

	@Override
	public KeyGenerator keyGenerator() {
		return new DefaultKeyGenerator();
	}

	
	
}

 

© 著作权归作者所有

lis1314
粉丝 11
博文 25
码字总数 24196
作品 0
丰台
高级程序员
私信 提问
加载中

评论(12)

a
alex197963
楼主你好我有个问题请教,
我的get方法没有参数,比如:
@Cacheable(value = "userConfig")
public UserConfigVo getUserConfig() {。。。
调用更新方法:
@CacheEvict(value = "userConfig")
public String updateUserConfig(。。。
之后为什么再次调用get方法还是会命中缓存呢
lis1314
lis1314

引用来自“styyxm”的评论

如果不用ehcache,我在java代码中怎么使用缓存呢?
不是用了Spring的Cache么。它的思想就是,给他提供一个实现了cacheble 接口的实现类,它就能够缓存。你使用的ehcache 里面有标准的org.springframework.cache.Cache实现类所以和spring cache结合到一块了。你想用什么缓存其实都是一样的。如果是redis,你看下spring data redis 集成。默认它是有实现类的。其他的缓存我不太清楚了,如果没有你只能自己实现org.springframework.cache.Cache
s
styyxm
如果不用ehcache,我在java代码中怎么使用缓存呢?
lis1314
lis1314

引用来自“styyxm”的评论

大神还有个问题“---------”下面是我之前就有的,但是加了“自定义后”就报错了,想问的是:加上自定义的后,之前的是不是就不用了。
<!--支持缓存注解-->
<cache:annotation-driven cache-manager="ehcacheManager" key-generator="defaultKeyGenerator"/>
<bean id="defaultKeyGenerator" class="com.framework.util.DefaultKeyGenerator"/>
------------------------------------------------------------------------------------------------------------------

  <bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
   <property name="configLocation" value="classpath:ehcache.xml" />
   <property name="shared" value="true"></property>
  </bean>
  <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
   <property name="cacheManager" ref="ehcacheManagerFactory" />
  </bean>
只能配置一个啊。你重复配置了?把之前的注释试一下吧。和ehcache没什么关系的啊,我之前redis,ehcache都有整合。主要是cache:annotation-driven 那一行
s
styyxm
大神还有个问题“---------”下面是我之前就有的,但是加了“自定义后”就报错了,想问的是:加上自定义的后,之前的是不是就不用了。
<!--支持缓存注解-->
<cache:annotation-driven cache-manager="ehcacheManager" key-generator="defaultKeyGenerator"/>
<bean id="defaultKeyGenerator" class="com.framework.util.DefaultKeyGenerator"/>
------------------------------------------------------------------------------------------------------------------

  <bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
   <property name="configLocation" value="classpath:ehcache.xml" />
   <property name="shared" value="true"></property>
  </bean>
  <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
   <property name="cacheManager" ref="ehcacheManagerFactory" />
  </bean>
s
styyxm
嗯嗯,所以@Cacheable(value="com.orm.pojo.CoreUser" )加它之后,默认就会对加注解的进行缓存了。
lis1314
lis1314

引用来自“styyxm”的评论

是不是默认就会自己调用,那原始key是不是就 被覆盖掉了。
在你自己缓存配置中加入XML配置。文章最后有写啊
s
styyxm
是不是默认就会自己调用,那原始key是不是就 被覆盖掉了。
s
styyxm
@Cacheable(value="com.orm.pojo.CoreUser" )//在这里面怎么调用defaultKeyGenerator
public void kk(object obj){}
s
styyxm
嗯嗯,很感谢你得文章,写的很好。
阿里巴巴开源框架-通用缓存访问JetCache介绍

JetCache是由阿里巴巴开源的通用缓存访问框架,如果你对Spring Cache很熟悉的话,请一定花一点时间了解一下JetCache,它更好用。 JetCache提供的核心能力包括: 提供统一的,类似jsr-107风格...

黄理
2018/01/24
0
0
Java 缓存系统 - JetCache

JetCache 是一个基于 Java 的缓存系统封装,提供统一的 API 和注解来简化缓存的使用。当前有四个实现,、(此部分未开源)、 (in memory) 和一个简易的 (in memory)。 主要特性: 通过统一的...

areyouok
2018/01/23
0
0
阿里巴巴开源的通用缓存访问框架JetCache介绍

JetCache是由阿里巴巴开源的通用缓存访问框架,如果你对Spring Cache很熟悉的话,请一定花一点时间了解一下JetCache,它更好用。 JetCache提供的核心能力包括: 提供统一的,类似jsr-107风格...

黄理
2018/04/24
0
0
搞懂分布式技术14:Spring Boot使用注解集成Redis缓存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a724888/article/details/80785403 为了提高性能,减少数据库的压力,使用缓存是非常好的手段之一。本文,讲解...

你的猫大哥
2018/06/23
0
0
Spring Boot(11)——使用Spring Cache

使用Spring Cache Spring提供了Cache抽象,它允许我们声明哪些bean的哪些方法的外部调用需要使用Cache。方法调用使用了Cache后,在调用真实方法前会先从缓存中获取结果,缓存中如果没有则会调...

elim168
2018/12/22
0
0

没有更多内容

加载失败,请刷新页面

加载更多

将博客搬至CSDN

https://blog.csdn.net/qq_38157006

Marhal
6分钟前
0
0
unicode Java中求字符串长度length()和codePointCount()的区别

在计算字符串长度时,Java的两种方法length()和codePointCount()一度让我困惑,运行书上例子得到的长度值是相等的,那为什么要设定两个方法呢? 对于普通字符串,这两种方法得到的值是一样的...

泉天下
7分钟前
0
0
uin-app 一、学习理由

选择uni-app 理由 别人的理由 1. 5+ 有HTML5+和Native.js技术,HTML5+包含常用的跨平台的几百个API,能满足常规开发需求,而Native.js把40w原生api映 射成js对象,这样js可以直接调原生。HTM...

轻轻的往前走
9分钟前
0
0
方括号及其在命令行中的不同用法介绍

通配 方括号最简单的用法就是通配。你可能在知道“ Globbing”这个概念之前就已经通过通配来匹配内容了,列出具有相同特征的多个文件就是一个很常见的场景,例如列出所有 JPEG 文件: ls *.j...

Linux就该这么学
14分钟前
0
0
vecty 基础

gopherjs 是把 go 编译为 js 的工具。 vecty 是基于 gopherjs 的一种类似 React 的开发框架。 安装 gopherjs 和 vecty go get -u github.com/gopherjs/gopherjsgo get -u github.com/gopher......

electricface
15分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部