Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我们如何在java中实现方法缓存_Java_Spring Aop - Fatal编程技术网

我们如何在java中实现方法缓存

我们如何在java中实现方法缓存,java,spring-aop,Java,Spring Aop,我想设计自己的注释,以便缓存从早期数据库调用检索到的结果 例如: public class CountryService { @MethodCache public List<Country> getCountries(); @MethodCache public Country getCountryById(int countryId); @InvalidateMethodCache public Country getCountryById(int country

我想设计自己的注释,以便缓存从早期数据库调用检索到的结果

例如:

public class CountryService {
 @MethodCache
 public List<Country> getCountries();

 @MethodCache
 public Country getCountryById(int countryId);

 @InvalidateMethodCache
 public Country getCountryById(int countryId);

}
公共类服务{
@方法缓存
公共列表国家();
@方法缓存
公共国家/地区getCountryById(国际国家/地区ID);
@无效方法缓存
公共国家/地区getCountryById(国际国家/地区ID);
}
我想对我的更多/所有方法使用这种类型的注释。我需要什么来实现这种类型的注释

@MethodCache:缓存方法结果。

@InvalidateMethodCache:清除缓存。

使用spring aop时的一个解决方案是创建一个方面来处理用自定义注释注释的所有方法。粗略的实现如下所示:

Map<String, Object> methodCache = new HahsMap<>();

@Around("execution(@(@com.mypack.MethodCache *) *)")
public Object cacheMethod(ProceedingJoinPoint pjp) {
     String cacheKey = getCacheKey(pjp);
     if ( methodCache.get(cacheKey)) {
          return methodCache.get(cacheKey);
     } else {
          Object result = pjp.proceed();
          methodCache.put(cacheKey, result);
          return result;
     }
}

private String getCacheKey(ProceedingJoinPoint pjp) {
     return pjp.getSignature().toString() + pjp.getTarget() + Arrays.asList(pjp.getArgs());
}
Map methodCache=new HahsMap();
@大约(“执行(@(@com.mypack.MethodCache*)*))
公共对象缓存方法(ProceedingJoinPoint pjp){
字符串cacheKey=getCacheKey(pjp);
if(methodCache.get(cacheKey)){
返回methodCache.get(cacheKey);
}否则{
对象结果=pjp.procedure();
put(cacheKey,result);
返回结果;
}
}
私有字符串getCacheKey(ProceedingJoinPoint pjp){
返回pjp.getSignature().toString()+pjp.getTarget()+Arrays.asList(pjp.getArgs());
}

如果有现成的注释,最好使用它们
虽然你可以遵循这一点,但我希望这能指导你

  • 实现接口CacheAnnotationParser
  • 扩展AnnotationCacheOperationSource,以便在内部解析器集合中除了Spring one之外,还可以放置自己的CacheAnnotationParser
  • 定义自定义注释CacheOperationSource以使用与Spring one相同的id,这样它将覆盖Spring internal。如果id匹配,它应该完全覆盖SpringOne。

    像这样

我已经使用spring/java实现了方法缓存


我已经实现了此功能并将我的项目上载到

例如:

public class CountryService {

    @MethodCache

    public List<Country> getCountries();


    @MethodCache

    public Country getCountryById(int countryId);

    @InvalidateMethodCache

    public Country deleteCountryByID(int countryId);

  }
公共类服务{
@方法缓存
公共列表国家();
@方法缓存
公共国家/地区getCountryById(国际国家/地区ID);
@无效方法缓存
公共国家/地区删除CountryById(国际国家/地区ID);
}

实际问题是什么?当我在任何方法中使用此注释时,它将缓存方法结果以供应用,下次我将使用相同的参数调用此方法时,它将不会调用DB,从缓存加载。如果您希望创建自定义注释,这里有一些参考资料,您可能应该使用本文中描述的现有解决方案。请阅读关于能够实现您所需要的功能的文章。Spring已经提供了-或者使用它,或者从Spring如何实现它中获得灵感,如果我可以添加一些建议。。。1) 不要使用
哈希映射
,而是使用
ConcurrentHashMap
,以便缓存支持并发请求。2) 通过@bean注释使这个并发映射成为Springbean。3) 将该映射自动关联到包含@Around方法的类(您还需要该类是Springbean)。