2
不同的NHibernate會話可能共享一個第一級緩存嗎?我試圖用攔截器和聽衆來實現它。除了Session.Evict()之外,所有的工作都很好。在不同會話之間共享一級緩存?
public class SharedCache :
EmptyInterceptor,
IFlushEntityEventListener,
ILoadEventListener,
IEvictEventListener,
ISharedCache {
[ThreadStatic]
private readonly Dictionary<string, Dictionary<object, object>> cache;
private ISessionFactory factory;
public SharedCache() {
this.cache = new Dictionary<string, Dictionary<object, object>>();
}
public override object Instantiate(string clazz, EntityMode entityMode, object id) {
var entityCache = this.GetCache(clazz);
if (entityCache.ContainsKey(id))
return entityCache[id];
var entity = Activator.CreateInstance(Type.GetType(clazz));
this.factory.GetClassMetadata(clazz).SetIdentifier(entity, id, entityMode);
return entity;
}
private Dictionary<object, object> GetCache(string clazz) {
if (!cache.ContainsKey(clazz))
cache.Add(clazz, new Dictionary<object, object>());
return cache[clazz];
}
public void Configure(Configuration config) {
config.SetInterceptor(this);
config.SetListener(ListenerType.FlushEntity, this);
config.SetListener(ListenerType.Load, this);
config.SetListener(ListenerType.Evict, this);
}
public void Initialize(ISessionFactory sessionFactory) {
this.factory = sessionFactory;
}
public void OnFlushEntity(FlushEntityEvent ev) {
var entry = ev.EntityEntry;
var entityCache = this.GetCache(ev.EntityEntry.EntityName);
if (entry.Status == Status.Deleted) {
entityCache.Remove(entry.Id);
return;
}
if (!entry.ExistsInDatabase && !entityCache.ContainsKey(entry.Id))
entityCache.Add(entry.Id, ev.Entity);
}
public void OnLoad(LoadEvent ev, LoadType loadType) {
var entityCache = this.GetCache(ev.EntityClassName);
if (entityCache.ContainsKey(ev.EntityId))
ev.Result = entityCache[ev.EntityId];
}
public void OnEvict(EvictEvent ev) {
var entityName = ev.Session.GetEntityName(ev.Entity);
var entityCache = this.GetCache(entityName);
var id = ev.Session.GetIdentifier(ev.Entity);
entityCache.Remove(id);
}
}
爲什麼? – 2009-09-18 14:49:29
通常由於nhibernate會話不是線程安全的。所以應該在每個線程中打開會話。但也需要一級緩存。所以我決定在會話中創建自己的第一級緩存。 – SHSE 2009-09-18 15:03:18
如果您需要跨會話緩存,爲什麼不使用第二級緩存? – 2009-09-18 19:10:36