nHibernate二级缓存,带有memcached和随机无效的强制转换

Rya*_*ger 7 nhibernate memcached caching asp.net-mvc-3

我有一个使用nHibernateMVC3应用程序和memcached作为二级缓存提供程序.我们间歇性地(但最近更频繁地)得到了奇怪的铸造问题.它随机发生,无效的memcached缓存将解决问题.

它只发生在我们的生产环境中,因为我们不在其他环境中运行memcached.但是我在本地运行memcached并试图让它在本地运行而没有运气.

我们在Windows上使用memcached 1.2.6.这是堆栈跟踪.我知道确定任何事情都不是足够的信息,但如果有人对我如何调试这个有任何想法,那将是值得赞赏的.我正在尝试在我们的生产机器上进行远程调试,但这是一年中的繁忙时期并且存在风险.

Unable to cast object of type 'System.Int32' to type 'System.String'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.]
   (Object , Object[] , SetterCallback ) +4270
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +80

[PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of MyApplication.Business.Data.Program]
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +207
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values) +97
   NHibernate.Cache.Entry.CacheEntry.Assemble(Object[] values, Object result, Object id, IEntityPersister persister, IInterceptor interceptor, ISessionImplementor session) +306
   NHibernate.Cache.Entry.CacheEntry.Assemble(Object instance, Object id, IEntityPersister persister, IInterceptor interceptor, ISessionImplementor session) +147
   NHibernate.Event.Default.DefaultLoadEventListener.AssembleCacheEntry(CacheEntry entry, Object id, IEntityPersister persister, LoadEvent event) +434
   NHibernate.Event.Default.DefaultLoadEventListener.LoadFromSecondLevelCache(LoadEvent event, IEntityPersister persister, LoadType options) +800
   NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) +560
   NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) +229
   NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) +438
   NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType) +943
   NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType) +99
   NHibernate.Impl.SessionImpl.Get(String entityName, Object id) +117
   NHibernate.Impl.SessionImpl.Get(Object id) +70
   MyApplication.Business.Repositories.Repository`1.Get(Object id) +148
Run Code Online (Sandbox Code Playgroud)

Tru*_*mpi 3

我们遇到了同样的问题,我们已将问题确定为二级缓存中的无效缓存条目。在我们的例子中,当我们从缓存在二级缓存中的表/实体中添加或删除列/属性并部署更改时,就会出现问题。该代码需要五列(为了论证),而缓存存储六列。

我们在存储库中采用的一种策略是捕获异常并使缓存无效。然后我们重试获取。

    public override T Get(int id)
    {
        try
        {
            return base.Get(id);
        }
        catch (InvalidCastException)
        {
            _sessionFactory.EvictEntity(typeof (T).Name);
            return Get(id);
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!