Skip to content

Commit

Permalink
增加命名空间(namespace),隔离各个系统间数据。
Browse files Browse the repository at this point in the history
  • Loading branch information
qiujiayu committed Sep 11, 2015
1 parent a8582a0 commit 37dee1a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.qiujiayu</groupId>
<artifactId>autoload-cache</artifactId>
<version>1.7</version>
<version>1.8</version>
<packaging>jar</packaging>
<name>AutoLoadCache</name>
<description>User Spring AOP and annotation to do with cache.</description>
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/com/jarvis/cache/memcache/CachePointCut.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,54 @@ public class CachePointCut extends AbstractCacheManager<Serializable> {

private MemcachedClient memcachedClient;

private String namespace;

public CachePointCut(AutoLoadConfig config) {
super(config);
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace=namespace;
}

private String appendNamespace(String cacheKey) {
if(null != namespace && namespace.length() > 0) {
return namespace + ":" + cacheKey;
}
return cacheKey;
}

@Override
public void setCache(String cacheKey, CacheWrapper<Serializable> result, int expire) {
result.setLastLoadTime(System.currentTimeMillis());
cacheKey=appendNamespace(cacheKey);
memcachedClient.set(cacheKey, expire, result);
}

@SuppressWarnings("unchecked")
@Override
public CacheWrapper<Serializable> get(String key) {
return (CacheWrapper<Serializable>)memcachedClient.get(key);
public CacheWrapper<Serializable> get(String cacheKey) {
cacheKey=appendNamespace(cacheKey);
return (CacheWrapper<Serializable>)memcachedClient.get(cacheKey);
}

/**
* 通过组成Key直接删除
* @param key
*/
@Override
public void delete(String key) {
if(null == memcachedClient || null == key) {
public void delete(String cacheKey) {
if(null == memcachedClient || null == cacheKey) {
return;
}
cacheKey=appendNamespace(cacheKey);
try {
memcachedClient.delete(key);
this.getAutoLoadHandler().resetAutoLoadLastLoadTime(key);
memcachedClient.delete(cacheKey);
this.getAutoLoadHandler().resetAutoLoadLastLoadTime(cacheKey);
} catch(Exception e) {
}
}
Expand Down Expand Up @@ -78,8 +98,8 @@ public void deleteDefinedCacheKey(String keySpEL, Object[] arguments) {
public void delete(List<String> keys) {
try {
if(null != keys && !keys.isEmpty()) {
for(String key: keys) {
this.delete(key);
for(String cacheKey: keys) {
this.delete(cacheKey);
}
}
} catch(Exception e) {
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/com/jarvis/cache/redis/ShardedCachePointCut.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,40 @@ public class ShardedCachePointCut extends AbstractCacheManager<Serializable> {

private ShardedJedisPool shardedJedisPool;

private String namespace;

public ShardedCachePointCut(AutoLoadConfig config) {
super(config);
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace=namespace;
}

private void returnResource(ShardedJedis shardedJedis) {
shardedJedis.close();
}

private String appendNamespace(String cacheKey) {
if(null != namespace && namespace.length() > 0) {
return namespace + ":" + cacheKey;
}
return cacheKey;
}

@Override
public void setCache(final String cacheKey, final CacheWrapper<Serializable> result, final int expire) {
public void setCache(String cacheKey, final CacheWrapper<Serializable> result, final int expire) {
if(null == shardedJedisPool || null == cacheKey) {
return;
}
if(cacheKey.indexOf("*") != -1 || cacheKey.indexOf("?") != -1) {
throw new java.lang.RuntimeException("cacheKey:" + cacheKey + "; has '*' or '?'");
}
cacheKey=appendNamespace(cacheKey);
ShardedJedis shardedJedis=null;
try {
result.setLastLoadTime(System.currentTimeMillis());
Expand All @@ -61,13 +79,14 @@ public void setCache(final String cacheKey, final CacheWrapper<Serializable> res

@SuppressWarnings("unchecked")
@Override
public CacheWrapper<Serializable> get(final String cacheKey) {
public CacheWrapper<Serializable> get(String cacheKey) {
if(null == shardedJedisPool || null == cacheKey) {
return null;
}
CacheWrapper<Serializable> res=null;
ShardedJedis shardedJedis=null;
try {
cacheKey=appendNamespace(cacheKey);
shardedJedis=shardedJedisPool.getResource();
Jedis jedis=shardedJedis.getShard(cacheKey);
byte bytes[]=jedis.get(keySerializer.serialize(cacheKey));
Expand Down Expand Up @@ -118,10 +137,11 @@ public void deleteDefinedCacheKey(String keySpEL, Object[] arguments) {
* @param cacheKey 如果传进来的值中 带有 * 或 ? 号,则会使用批量删除(遍历所有Redis服务器)
*/
@Override
public void delete(final String cacheKey) {
public void delete(String cacheKey) {
if(null == shardedJedisPool || null == cacheKey) {
return;
}
cacheKey=appendNamespace(cacheKey);
final AutoLoadHandler<Serializable> autoLoadHandler=this.getAutoLoadHandler();
ShardedJedis shardedJedis=null;
if(cacheKey.indexOf("*") != -1 || cacheKey.indexOf("?") != -1) {// 如果是批量删除缓存,则要遍历所有redis,避免遗漏。
Expand Down

0 comments on commit 37dee1a

Please sign in to comment.