Merge pull request #116 from LucaLq/main

feat[orm]: add an interface to persist the specified cache
This commit is contained in:
godotg
2024-06-21 14:00:14 +08:00
committed by GitHub
2 changed files with 30 additions and 1 deletions
+23 -1
View File
@@ -240,12 +240,34 @@ public class EntityCache<PK extends Comparable<PK>, E extends IEntity<PK>> imple
}
}
});
persistList(updateList, entityClass);
}
@Override
public void persist(PK pk) {
var pnode = cache.get(pk);
if (pnode == null) {
return;
}
@SuppressWarnings("unchecked")
var entityClass = (Class<E>) entityDef.getClazz();
var updateList = new ArrayList<E>();
var currentTime = TimeUtils.currentTimeMillis();
if (pnode.getModifiedTime() == pnode.getWriteToDbTime()) {
return;
}
var entity = pnode.getEntity();
pnode.setWriteToDbTime(currentTime);
pnode.setModifiedTime(currentTime);
updateList.add(entity);
persistList(updateList, entityClass);
}
private void persistList(ArrayList<E> updateList, Class<E> entityClass) {
// 执行更新
if (updateList.isEmpty()) {
return;
}
var page = Page.valueOf(1, BATCH_SIZE, updateList.size());
var maxPageSize = page.totalPage();
@@ -66,6 +66,13 @@ public interface IEntityCache<PK extends Comparable<PK>, E extends IEntity<PK>>
*/
void invalidate(PK pk);
/**
* 持久化缓存数据
*
* @param pk 主键
*/
void persist(PK pk);
/**
* 持久化所有缓存数据
*/