mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-07 22:22:19 +00:00
feat[orm]: add loadAndInit method
This commit is contained in:
@@ -129,6 +129,29 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E loadOrInit(PK pk) {
|
||||
AssertionUtils.notNull(pk);
|
||||
var pnode = cache.get(pk);
|
||||
if (pnode != null) {
|
||||
return pnode.getEntity();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
var entity = (E) OrmContext.getAccessor().load(pk, (Class<IEntity<?>>) entityDef.getClazz());
|
||||
|
||||
// 如果数据库中不存在则给一个默认值
|
||||
if (entity == null) {
|
||||
// 数据库无法加载缓存,返回默认值
|
||||
entity = (E) entityDef.newEntity(pk);
|
||||
entity.setId(pk);
|
||||
OrmContext.getAccessor().insert(entity);
|
||||
}
|
||||
pnode = new PNode<>(entity);
|
||||
cache.put(pk, pnode);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验需要更新的entity和缓存的entity是否为同一个entity
|
||||
*/
|
||||
|
||||
@@ -29,6 +29,14 @@ public interface IEntityCache<PK extends Comparable<PK>, E extends IEntity<PK>>
|
||||
*/
|
||||
E load(PK pk);
|
||||
|
||||
/**
|
||||
* EN: Load data from the database into the cache, and if the database does not exist,
|
||||
* return a default example with the id as the passed in value. And put it into storage.
|
||||
* <p>
|
||||
* CN: 从数据库中加载数据到缓存,如果数据库不存在则返回一个id为传入值的默认示例。并且入库。(设置了索引唯一的无法使用该方法)
|
||||
*/
|
||||
E loadOrInit(PK pk);
|
||||
|
||||
/**
|
||||
* 更新缓存中的数据,只更新缓存的时间戳,并通过一定策略写入到数据库
|
||||
* <p>
|
||||
|
||||
@@ -24,7 +24,10 @@ public interface IEntity<PK extends Comparable<PK>> {
|
||||
*/
|
||||
PK id();
|
||||
|
||||
|
||||
/**
|
||||
* 设置主键
|
||||
*/
|
||||
void setId(PK pk);
|
||||
/**
|
||||
* 一个文档的写入到数据库的version版本,version的get和set方法
|
||||
* <p>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.zfoo.orm.cache;
|
||||
|
||||
import com.zfoo.orm.anno.EntityCacheAutowired;
|
||||
import com.zfoo.orm.entity.MailEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author:awakeyoyoyo
|
||||
* @Date:2024/5/23 16:32
|
||||
*/
|
||||
@Component
|
||||
public class EmailManager {
|
||||
|
||||
@EntityCacheAutowired
|
||||
public IEntityCache<String, MailEntity> mailEntityCaches;
|
||||
}
|
||||
@@ -15,6 +15,7 @@ package com.zfoo.orm.cache;
|
||||
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.zfoo.orm.OrmContext;
|
||||
import com.zfoo.orm.entity.MailEntity;
|
||||
import com.zfoo.orm.entity.UserEntity;
|
||||
import com.zfoo.protocol.util.ThreadUtils;
|
||||
import com.zfoo.scheduler.util.TimeUtils;
|
||||
@@ -51,6 +52,24 @@ public class EntityCachesTest {
|
||||
userEntityCaches.load(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadOrInsert() {
|
||||
var context = new ClassPathXmlApplicationContext("application.xml");
|
||||
|
||||
// 动态去拿到UserEntity的EntityCaches
|
||||
@SuppressWarnings("unchecked")
|
||||
var mailEntityCaches = (IEntityCache<String, MailEntity>) OrmContext.getOrmManager().getEntityCaches(MailEntity.class);
|
||||
|
||||
for (var i = 1; i <= 10; i++) {
|
||||
var entity = mailEntityCaches.loadOrInit(String.valueOf(i));
|
||||
entity.setContent("msg:" + i);
|
||||
mailEntityCaches.update(entity);
|
||||
}
|
||||
|
||||
ThreadUtils.sleep(60 * TimeUtils.MILLIS_PER_SECOND);
|
||||
|
||||
mailEntityCaches.load("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void collectionTest() {
|
||||
|
||||
@@ -68,6 +68,11 @@ public class UserEntity implements IEntity<Long> {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(Long id) {
|
||||
this.id=id;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,11 @@ public class MapEntity implements IEntity<Long> {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(Long id) {
|
||||
this.id=id;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user