转自:http://haoshenqi.blogchina.com/1538799.html
Memcached-----memcached实现内存缓存
# tar -xzf libevent-1.1a.tar.gz
# cd libevent-1.1a
# ./configure --prefix=/usr
# make
# make install
# cd ..
# tar -xzf memcached-1.1.12.tar.gz
# cd memcached-1.1.12
# ./configure --prefix=/usr
# make
# make install
-d 以守护程序(daemon)方式运行 memcached;
-m 设置 memcached 可以使用的内存大小,单位为 M;
-l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;
-p 设置监听的端口,默认为 11211,所以也可以不设置此参数;
-u 指定用户,如果当前为 root 的话,需要使用此参数指定用户。
-m 设置 memcached 可以使用的内存大小,单位为 M;
-l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;
-p 设置监听的端口,默认为 11211,所以也可以不设置此参数;
-u 指定用户,如果当前为 root 的话,需要使用此参数指定用户。
public abstract class BaseManager implements Manager
...
{
private MemcachedClient memcached;
protected int default_cache_time_second = 3600;
public void setMemcached(MemcachedClient memcached) ...{
this.memcached = memcached;
}
public MemcachedClient getMemcached() ...{
return memcached;
}
public void setDefault_cache_time_second(int default_cache_time_second) ...{
this.default_cache_time_second = default_cache_time_second;
}
public Object getCacheValueFromMemcached(String key) ...{
return getCacheValueFromMemcached(key, null);
}
public Object getCacheValueFromMemcached(String key, Object obj) ...{
Object new_obj = null;
try ...{
new_obj = memcached.get(key);
} catch (Exception e) ...{
if (logger.isWarnEnabled()) ...{
logger.warn("Failed to get from MeMCache", e);
}
}
return (new_obj != null) ? new_obj : obj;
}
public void setCacheValueToMemcached(String cacheKey, int time_to_live, Serializable obj) ...{
if (null != memcached.get(cacheKey)) ...{
memcached.replace(cacheKey, time_to_live, obj);
} else ...{
memcached.add(cacheKey, time_to_live, obj);
}
}
}
private MemcachedClient memcached;
protected int default_cache_time_second = 3600;
public void setMemcached(MemcachedClient memcached) ...{
this.memcached = memcached;
}
public MemcachedClient getMemcached() ...{
return memcached;
}
public void setDefault_cache_time_second(int default_cache_time_second) ...{
this.default_cache_time_second = default_cache_time_second;
}
public Object getCacheValueFromMemcached(String key) ...{
return getCacheValueFromMemcached(key, null);
}
public Object getCacheValueFromMemcached(String key, Object obj) ...{
Object new_obj = null;
try ...{
new_obj = memcached.get(key);
} catch (Exception e) ...{
if (logger.isWarnEnabled()) ...{
logger.warn("Failed to get from MeMCache", e);
}
}
return (new_obj != null) ? new_obj : obj;
}
public void setCacheValueToMemcached(String cacheKey, int time_to_live, Serializable obj) ...{
if (null != memcached.get(cacheKey)) ...{
memcached.replace(cacheKey, time_to_live, obj);
} else ...{
memcached.add(cacheKey, time_to_live, obj);
}
}
}
public List getLastModifyAlbumMember(int limit)
...
{
String cacheKey = this.createCachekey(new
Object[]...{"schedule","lastmodifyalbumember",limit});
List list = (List) this.getCacheValueFromMemcached(cacheKey);
List list1 = new ArrayList();
if(null!=list&&list.size()>0)...{
for (Object aList : list) ...{
list1.add(Integer.parseInt(String.valueOf(aList)));
}
}
return list1;
}
String cacheKey = this.createCachekey(new
Object[]...{"schedule","lastmodifyalbumember",limit});
List list = (List) this.getCacheValueFromMemcached(cacheKey);
List list1 = new ArrayList();
if(null!=list&&list.size()>0)...{
for (Object aList : list) ...{
list1.add(Integer.parseInt(String.valueOf(aList)));
}
}
return list1;
}