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