GoFrameworkフレームワーク概要(五)キャッシュ編
5638 ワード
Redisキャッシュ
フレームワークはRedisに対して基礎的な呼び出しパッケージを行い、インタフェースクラスは:org.go.framework.cache.CacheServiceは、多くの方法で使用できます.Dubboサービス側およびWeb側は、このサービスインスタンスを直接参照することができる.
public void put(String key, Object value, int timeToLive, TimeUnit timeUnit, String namespace);
public void put(String key, Object value, int timeToIdle, int timeToLive, TimeUnit timeUnit, String namespace);
public boolean exists(String key, String namespace);
public Object get(String key, String namespace);
public Object get(String key, String namespace, int timeToIdle, TimeUnit timeUnit);
public void delete(String key, String namespace);
public void mput(Map map, int timeToLive, TimeUnit timeUnit, String namespace);
public Map mget(Collection keys, String namespace);
public void mdelete(Collection keys, String namespace);
public boolean containsKey(String key, String namespace);
public boolean putIfAbsent(String key, Object value, int timeToLive, TimeUnit timeUnit, String namespace);
public long increment(String key, long delta, int timeToLive, TimeUnit timeUnit, String namespace);
public boolean supportsTimeToIdle();
public boolean supportsUpdateTimeToLive();
public void invalidate(String namespace);
また、キャッシュテンプレートの抽象クラスorgも提供する.go.framework.cache.CacheServiceは、キャッシュ機能を迅速に実現します.
public abstract class AbstractBaseRedisCache extends AbstractDubboIntegrationService implements IDubboCache{
/**
*
* @return
*/
protected abstract Integer getExpiredMinutes();
/**
*
* @param cacheKey
* @return
* @throws PendingException
*/
protected abstract V query(K cacheKey) throws PendingException;
/**
*
* @return
*/
protected abstract ResCode getErrResCode();
/**
*
* @return
*/
protected abstract String getNameSpace();
/**
* , 0
* ,
* @return
*/
protected abstract int getVersion();
@Autowired
private CacheService cacheService;
/**
* Key
* @param key Key
* @return
* @throws PendingException
*/
@SuppressWarnings("unchecked")
public V get(K key) throws PendingException{
try {
//
String adjustNameSpace = getNameSpace() + getVersion();
// redis
CacheContainer result = (CacheContainer) cacheService.get(String.valueOf(key), adjustNameSpace);
if(result == null){
info(" , 。");
result = new CacheContainer(query(key));
cacheService.put(String.valueOf(key), result, getExpiredMinutes(), TimeUnit.MINUTES, adjustNameSpace);
}
return result.getObject();
} catch (Exception ex) {
error(" !", ex);
throw new PendingException(getErrResCode().getCode(), getErrResCode().getInfo());
}
}
/**
*
* @param key
* @throws PendingException
*/
public void clearCache(K key) throws PendingException{
try {
//
String adjustNameSpace = getNameSpace() + getVersion();
// redis
cacheService.delete(String.valueOf(key), adjustNameSpace);
}catch (Exception ex) {
error(" !", ex);
ResCode.REDIS_CACHE_DEL_ERR.throwException();
}
}
...
}
実装例:
@Component
public class UserInfoRedisCache extends AbstractBaseRedisCache implements IUserInfoRedisCache {
@Reference(version = "1.0.0")
private UserInfoFacade userInfoFacade;
@Override
protected int getVersion() {
return 0; //
}
@Override
protected String getNameSpace() {
return CacheNameSpaceConstants.User.USER_INFO_CACHE;
}
@Override
protected Integer getExpiredMinutes() {
return 60 * 24;//
}
@Override
protected ResCode getErrResCode() {
return ResCode.userInfoCacheQueryFailed;
}
@Override
protected UserInfoItem query(Integer userId) throws PendingException {
//
UserInfoRspDto userInfoResDto = getUserInfoById(userId);
//
return BeanMapping.map(userInfoResDto, UserInfoItem.class);
}
/**
* Id
*
* @param userId
* @return
* @throws PendingException
*/
private UserInfoRspDto getUserInfoById(Integer userId) throws PendingException {
//
GetUserInfoReqDto getUserInfoReqDto = new GetUserInfoReqDto();
getUserInfoReqDto.setUserId(userId);
// dubbo
UserInfoRspDto userInfoResDto = userInfoFacade.getUserInfo(getUserInfoReqDto);
// ,
userInfoResDto.throwExceptionIfFailed();
return userInfoResDto;
}
}
ローカルキャッシュ
ローカルキャッシュのテンプレート抽象クラスはorg.go.framework.cache.CacheServiceは,実現メカニズムの原理がredisとあまり差がなく,Guavaの下位キャッシュフレームワークを用いて実現している.
ローカルキャッシュのパフォーマンスはRedisキャッシュよりはるかに高いので、優先的に使用することをお勧めします.ただし、ローカルキャッシュはそれぞれの仮想マシンに格納されているため、マルチノードの導入時にデータ同期更新の問題を考慮する必要があります.
転載先:https://www.cnblogs.com/wuyuhuanzhen/p/9285038.html