memcache 的使用方法:
一个配置文件:
一个memcache的工具类
public final class MemcachedUtils{
/**
* memcached客户端单例
*/
private static MemCachedClient cachedClient = new MemCachedClient();
private MemcachedUtils(){
}
public static boolean add(String key, Object value) {
return cachedClient.add(key, value);
}
public static boolean add(String key, Object value, Integer expire) {
return cachedClient.add(key, value, expire);
}
public static boolean put(String key, Object value) {
return cachedClient.set(key, value);
}
public static boolean put(String key, Object value, Integer expire) {
return cachedClient.set(key, value, expire);
}
public static boolean replace(String key, Object value) {
return cachedClient.replace(key, value);
}
public static boolean replace(String key, Object value, Integer expire) {
return cachedClient.replace(key, value, expire);
}
public static Object get(String key) {
return cachedClient.get(key);
}
}
应用:
在service 的实现类使用
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
public List<Student> findGoodStudent() {
List<Student> goodStudents;
if(MemcachedUtils.get("goodStudents") != null){
goodStudents = (List<Student>) MemcachedUtils.get("goodStudents");
System.out.println("使用了memcache");
return goodStudents;
}
goodStudents = studentMapper.findGoodStudent();
System.out.println("没有使用memcache");
MemcachedUtils.add("goodStudents",goodStudents);
return goodStudents;
}
这是我的使用方法,我想请教一下大佬们在实际工作中的使用场景和方法是如何做的???
慕的地10843
慕丝7291255
手掌心
相关分类