This commit is contained in:
redkale
2023-09-22 23:29:55 +08:00
parent e41af85715
commit 1c7bcdebc7
3 changed files with 20 additions and 5 deletions

View File

@@ -51,7 +51,11 @@ public @interface Cacheable {
*/
int interval() default 0;
@Deprecated
/**
* (Optional) DataSource是否直接返回对象的真实引用 而不是copy一份
*
* @return boolean
*/
boolean direct() default false;
@Deprecated

View File

@@ -81,17 +81,28 @@ public final class EntityCache<T> {
private CompletableFuture<List<T>> loadFuture;
public EntityCache(final EntityInfo<T> info, final Cacheable c) {
this(info, c != null ? c.interval() : 0);
this(info, c != null ? c.interval() : 0, c != null && c.direct());
}
EntityCache(final EntityInfo<T> info, final int cacheInterval) {
EntityCache(final EntityInfo<T> info, final int cacheInterval, final boolean cacheDirect) {
this.info = info;
this.interval = cacheInterval < 0 ? 0 : cacheInterval;
this.type = info.getType();
this.arrayer = info.getArrayer();
this.creator = info.getCreator();
this.primary = info.primary;
this.needCopy = true;
org.redkale.persistence.VirtualEntity ve = info.getType().getAnnotation(org.redkale.persistence.VirtualEntity.class);
boolean direct = cacheDirect;
if (!direct) {
direct = ve != null && ve.direct();
}
{ //兼容废弃类
org.redkale.source.VirtualEntity ve2 = info.getType().getAnnotation(org.redkale.source.VirtualEntity.class);
if (!direct && ve2 != null) {
direct = ve2.direct();
}
}
this.needCopy = !direct;
this.newCopier = Copier.create(type, type, (e, c) -> {
try {
return e.getAnnotation(Transient.class) == null && e.getAnnotation(javax.persistence.Transient.class) == null;

View File

@@ -687,7 +687,7 @@ public final class EntityInfo<T> {
Cacheable c1 = type.getAnnotation(Cacheable.class);
javax.persistence.Cacheable c2 = type.getAnnotation(javax.persistence.Cacheable.class);
if (this.table == null || (!cacheForbidden && c1 != null && c1.value()) || (!cacheForbidden && c2 != null && c2.value())) {
this.cache = new EntityCache<>(this, c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval());
this.cache = new EntityCache<>(this, c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval(), c1 == null ? (c2 == null ? false : c2.direct()) : c1.direct());
} else {
this.cache = null;
}