diff --git a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java index eebd91f6..54bfed6a 100644 --- a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java +++ b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java @@ -54,6 +54,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -143,6 +144,11 @@ public class OrmManager implements IOrmManager { if (!hasIndex) { var indexOptions = new IndexOptions(); indexOptions.unique(index.isUnique()); + + if(index.isTtl()){ + indexOptions.expireAfter(index.getExpireAfterSeconds(), TimeUnit.SECONDS) ; + } + if (index.isAscending()) { collection.createIndex(Indexes.ascending(fieldName), indexOptions); } else { @@ -326,7 +332,16 @@ public class OrmManager implements IOrmManager { var fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Index.class); for (var field : fields) { var indexAnnotation = field.getAnnotation(Index.class); - IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique()); + + if(indexAnnotation.ttl()) { + if (!field.getGenericType().toString().equals( + "class java.util.Date")){ + throw new IllegalArgumentException(StringUtils.format("[{}]不是Date类型", field.getName())); + } + + } + + IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique(),indexAnnotation.ttl(),indexAnnotation.expireAfterSeconds()); indexDefMap.put(field.getName(), indexDef); } diff --git a/orm/src/main/java/com/zfoo/orm/model/anno/Index.java b/orm/src/main/java/com/zfoo/orm/model/anno/Index.java index 116467a9..4ef1b456 100644 --- a/orm/src/main/java/com/zfoo/orm/model/anno/Index.java +++ b/orm/src/main/java/com/zfoo/orm/model/anno/Index.java @@ -26,4 +26,8 @@ public @interface Index { boolean ascending(); boolean unique(); + + boolean ttl() default false;; + + long expireAfterSeconds() default 0L; } diff --git a/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java b/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java index 3214b234..86133fb4 100644 --- a/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java +++ b/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java @@ -25,10 +25,17 @@ public class IndexDef { private boolean ascending; private boolean unique; - public IndexDef(Field field, boolean ascending, boolean unique) { + private boolean ttl; + + private long expireAfterSeconds; + + + public IndexDef(Field field, boolean ascending, boolean unique, boolean ttl, long expireAfterSeconds) { this.field = field; this.ascending = ascending; this.unique = unique; + this.ttl = ttl; + this.expireAfterSeconds = expireAfterSeconds; } public Field getField() { @@ -54,4 +61,20 @@ public class IndexDef { public void setUnique(boolean unique) { this.unique = unique; } + + public boolean isTtl() { + return ttl; + } + + public void setTtl(boolean ttl) { + this.ttl = ttl; + } + + public long getExpireAfterSeconds() { + return expireAfterSeconds; + } + + public void setExpireAfterSeconds(long expireAfterSeconds) { + this.expireAfterSeconds = expireAfterSeconds; + } }