createCache无效
- Dominant language
- Java
- Stars
- 5.6k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
你好,我在一个类里面声明了4个cache对象,如下
```
@Component
class CacheObj {
@CreateCache(name = CacheKey.VISIBLE_DATA_ACCESS,cacheType = CacheType.BOTH)
lateinit var visibleDataCache: Cache
@CreateCache(name = CacheKey.SUBSCRIPT, cacheType = CacheType.LOCAL)
lateinit var subscriptCache: Cache
@CreateCache(name = CacheKey.ORBIT_TRACK, cacheType = CacheType.LOCAL, expire = 10, timeUnit = TimeUnit.MINUTES)
lateinit var orbitTrackCache: Cache
@CreateCache(name = CacheKey.YC_PARAM,cacheType = CacheType.LOCAL)
lateinit var ycCache: Cache
}
```
但是使用的时候却发现这4个cache都为null
通过debug源码 发现在 `CreateCacheAnnotationBeanPostProcessor`类中
```
@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
beanFactory.registerDependentBean(beanName, "globalCacheConfig");
LazyInitCache cache = new LazyInitCache(beanFactory, ann, field);
field.setAccessible(true);
field.set(bean, cache);
}
```
是有初始化该Bean的这4个对象并给他们赋值了`LazyInitCache `但是当使用的时候却发现这四个属性为空,并且`LazyInitCache`的`checkInit()` 方法一直没有被调用,不知道出了什么问题,为什么这个bean的属性被置空,这是我调用的地方
```
@Aspect
@Component
class CacheAop {
@Autowired
lateinit var cacheObj: CacheObj
@Pointcut("@annotation(com.waytogalaxy.display.common.config.cacheConfig.CacheClear)")
fun findCacheClear() {
}
/**
* 清理对应的name属性的缓存
*/
@Before("findCacheClear()")
fun doClear(joinPoint: JoinPoint) {
val signature = joinPoint.signature
if (signature is MethodSignature) {
val method = signature.method
val cacheClear = method.getAnnotation(CacheClear::class.java)
val cacheName = cacheClear.name
val cache = findMatchNameCache( cacheName) ?: return
cache.clear(cacheName)
}
}
/**
* 获取缓存名字对应的缓存对象
* @param cacheName 缓存名称
*/
private fun findMatchNameCache(cacheName: String): Cache<*, *>? {
val fields = CacheObj::class.java.declaredFields
val field = fields?.firstOrNull {
val anno = it.getAnnotation(CreateCache::class.java)
anno.name == cacheName
}
if (field != null && field.type == Cache::class.java) {
return field.get(cacheObj) as Cache<*, *>
} else{
return null
}
}
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with CreateCacheAnnotationBeanPostProcessor.inject and LazyInitCache.checkInit, then trace how CacheObj is created and accessed from CacheAop.findMatchNameCache. Reproduce the reported Kotlin case and verify that each annotated field remains usable through reflection and that the cache initializes when accessed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin, spring
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100