废弃Cacheable类

This commit is contained in:
redkale
2023-12-07 21:59:51 +08:00
parent 8f6098834f
commit 64c3a2c4bd
5 changed files with 71 additions and 20 deletions

View File

@@ -31,8 +31,11 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* <code>Cacheable(false)</code> means that the entity and its state must * <code>Cacheable(false)</code> means that the entity and its state must
* not be cached by the provider. * not be cached by the provider.
* *
* @deprecated replace by {@link org.redkale.persistence.Entity#cacheable() }
*
* @since Java Persistence 2.0 * @since Java Persistence 2.0
*/ */
@Deprecated(since = "2.8.0")
@Target({TYPE}) @Target({TYPE})
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface Cacheable { public @interface Cacheable {

View File

@@ -1,4 +1,4 @@
/******************************************************************************* /** *****************************************************************************
* Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved.
* *
* This program and the accompanying materials are made available under the * This program and the accompanying materials are made available under the
@@ -12,7 +12,7 @@
* Linda DeMichiel - Java Persistence 2.1 * Linda DeMichiel - Java Persistence 2.1
* Linda DeMichiel - Java Persistence 2.0 * Linda DeMichiel - Java Persistence 2.0
* *
******************************************************************************/ ***************************************************************************** */
package org.redkale.persistence; package org.redkale.persistence;
import java.lang.annotation.*; import java.lang.annotation.*;
@@ -22,7 +22,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** /**
* Specifies that the class is an entity. This annotation is applied to the * Specifies that the class is an entity. This annotation is applied to the
* entity class. * entity class.
* *
* @since Java Persistence 1.0 * @since Java Persistence 1.0
*/ */
@Inherited @Inherited
@@ -31,19 +31,41 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface Entity { public @interface Entity {
/** /**
* (Optional) The entity name. Defaults to the unqualified * (Optional) The entity name. Defaults to the unqualified
* name of the entity class. This name is used to refer to the * name of the entity class. This name is used to refer to the
* entity in queries. The name must not be a reserved literal * entity in queries. The name must not be a reserved literal
* in the Java Persistence query language. * in the Java Persistence query language.
*
* @return String * @return String
*/ */
String name() default ""; String name() default "";
/** /**
* (Optional) The comment of the entity. * (Optional) The comment of the entity.
* *
* @return String * @return String
*/ */
String comment() default ""; String comment() default "";
/**
* (Optional) 是否缓存实体对象
*
* @return boolean
*/
boolean cacheable() default false;
/**
* (Optional) 定时自动更新缓存的周期秒数为0表示不做定时更新 大于0表示每经过interval秒后会自动从数据库中拉取数据更新Cache
*
* @return int
*/
int cacheInterval() default 0;
/**
* (Optional) DataSource是否直接返回对象的真实引用 而不是copy一份
*
* @return boolean
*/
boolean cacheDirect() default false;
} }

View File

@@ -687,10 +687,37 @@ public final class EntityInfo<T> {
this.updateQuestionPrepareCaseSQLs = null; this.updateQuestionPrepareCaseSQLs = null;
} }
//----------------cache-------------- //----------------cache--------------
Cacheable c1 = type.getAnnotation(Cacheable.class); boolean cacheable = false;
javax.persistence.Cacheable c2 = type.getAnnotation(javax.persistence.Cacheable.class); int interval = 0;
if (this.table == null || (!cacheForbidden && c1 != null && c1.value()) || (!cacheForbidden && c2 != null && c2.value())) { boolean direct = false;
this.cache = new EntityCache<>(this, c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval(), c1 == null ? (c2 != null && c2.direct()) : c1.direct()); org.redkale.persistence.Entity en = type.getAnnotation(org.redkale.persistence.Entity.class);
if (en != null) {
cacheable = en.cacheable();
interval = en.cacheInterval();
direct = en.cacheDirect();
} else {
org.redkale.persistence.VirtualEntity ve = type.getAnnotation(org.redkale.persistence.VirtualEntity.class);
if (ve != null) {
cacheable = true;
direct = ve.direct();
}
}
{ //兼容旧类
org.redkale.persistence.Cacheable c1 = type.getAnnotation(org.redkale.persistence.Cacheable.class);
if (c1 != null) {
cacheable = c1.value();
interval = c1.interval();
direct = c1.direct();
}
javax.persistence.Cacheable c2 = type.getAnnotation(javax.persistence.Cacheable.class);
if (c2 != null) {
cacheable = c2.value();
interval = c2.interval();
direct = c2.direct();
}
}
if (this.table == null || (!cacheForbidden && cacheable)) {
this.cache = new EntityCache<>(this, interval, direct);
} else { } else {
this.cache = null; this.cache = null;
} }

View File

@@ -12,7 +12,7 @@ import java.util.function.*;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.redkale.annotation.AutoLoad; import org.redkale.annotation.AutoLoad;
import org.redkale.convert.json.*; import org.redkale.convert.json.*;
import org.redkale.persistence.Cacheable; import org.redkale.persistence.Entity;
import org.redkale.persistence.Id; import org.redkale.persistence.Id;
import org.redkale.persistence.Transient; import org.redkale.persistence.Transient;
import org.redkale.source.*; import org.redkale.source.*;
@@ -187,7 +187,7 @@ public class FilterNodeTest {
} }
@AutoLoad @AutoLoad
@Cacheable @Entity(cacheable = true)
public static class CarTestTable { public static class CarTestTable {
public static List<CarTestTable> createList() { public static List<CarTestTable> createList() {
@@ -280,7 +280,7 @@ public class FilterNodeTest {
} }
@AutoLoad @AutoLoad
@Cacheable @Entity(cacheable = true)
public static class CarTypeTable { public static class CarTypeTable {
public static List<CarTypeTable> createList() { public static List<CarTypeTable> createList() {
@@ -337,7 +337,7 @@ public class FilterNodeTest {
} }
@AutoLoad @AutoLoad
@Cacheable @Entity(cacheable = true)
public static class UserTestTable { public static class UserTestTable {
public static List<UserTestTable> createList() { public static List<UserTestTable> createList() {

View File

@@ -86,7 +86,6 @@ public class TestSourceCache {
} }
@VirtualEntity @VirtualEntity
@Cacheable
public static class TestEntity { public static class TestEntity {
@Id @Id