This commit is contained in:
jinyu 2018-03-19 21:57:13 +08:00
parent 3779c33046
commit 774eb85b75
2 changed files with 17 additions and 13 deletions

View File

@ -18,7 +18,8 @@ import com.whalin.MemCached.SockIOPool;
* @since JDK 1.6 * @since JDK 1.6
* @see * @see
*/ */
public class MemcacheCacheStorager<T extends Cacheable> implements CacheStorager<T> { public class MemcacheCacheStorager<T extends Cacheable> implements
CacheStorager<T> {
private final MemCachedClient mc; private final MemCachedClient mc;
@ -29,13 +30,17 @@ public class MemcacheCacheStorager<T extends Cacheable> implements CacheStorager
public MemcacheCacheStorager(MemcachePoolConfig poolConfig) { public MemcacheCacheStorager(MemcachePoolConfig poolConfig) {
mc = new MemCachedClient(); mc = new MemCachedClient();
poolConfig.initSocketIO(); poolConfig.initSocketIO();
init(); initializeKey();
} }
private void init() { @SuppressWarnings("unchecked")
if (!mc.keyExists(ALLKEY)) { private Set<String> initializeKey() {
mc.set(ALLKEY, new HashSet<String>()); Set<String> all = (Set<String>) mc.get(ALLKEY);
if (all == null) {
all = new HashSet<String>();
mc.set(ALLKEY, all);
} }
return all;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -44,34 +49,33 @@ public class MemcacheCacheStorager<T extends Cacheable> implements CacheStorager
return (T) mc.get(key); return (T) mc.get(key);
} }
@SuppressWarnings("unchecked")
@Override @Override
public void caching(String key, T cache) { public void caching(String key, T cache) {
if (cache.getCreateTime() > 0l) { if (cache.getCreateTime() > 0l) {
mc.set(key, cache, new Date(cache.getCreateTime() + cache.getExpires() - CUTMS)); mc.set(key,
cache,
new Date(cache.getCreateTime() + cache.getExpires() - CUTMS));
} else { } else {
mc.set(key, cache); mc.set(key, cache);
} }
Set<String> all = (Set<String>) mc.get(ALLKEY); Set<String> all = initializeKey();
all.add(key); all.add(key);
mc.set(ALLKEY, all); mc.set(ALLKEY, all);
} }
@SuppressWarnings("unchecked")
@Override @Override
public T evict(String key) { public T evict(String key) {
T cache = lookup(key); T cache = lookup(key);
mc.delete(key); mc.delete(key);
Set<String> all = (Set<String>) mc.get(ALLKEY); Set<String> all = initializeKey();
all.remove(key); all.remove(key);
mc.set(ALLKEY, all); mc.set(ALLKEY, all);
return cache; return cache;
} }
@SuppressWarnings("unchecked")
@Override @Override
public void clear() { public void clear() {
Set<String> all = (Set<String>) mc.get(ALLKEY); Set<String> all = initializeKey();
for (String key : all) { for (String key : all) {
mc.delete(key); mc.delete(key);
} }

View File

@ -39,7 +39,7 @@ public final class HttpParams {
* connectTimeout = 15000,readTimeout=20000 * connectTimeout = 15000,readTimeout=20000
*/ */
public HttpParams() { public HttpParams() {
this(15000, 20000); this(5000, 15000);
} }
public HttpParams(int connectTimeout, int readTimeout) { public HttpParams(int connectTimeout, int readTimeout) {