TargetProcess Development Tricks: Clear NHiberante cache using ASP.NET handler (.ashx) | Targetprocess - Enterprise Agility Solution
16 years ago

TargetProcess Development Tricks: Clear NHiberante cache using ASP.NET handler (.ashx)

Sometimes we need to clear cache of NHibernate. For example when the database was changed without using NHibernate. We have created the corresponding handler in web application ClearCache.ashx with the following code:

<%@ WebHandler Language="C#" Class="ClearCache" %>
using System;
using System.Collections;
using System.Web;
using NHibernate;

public class ClearCache : IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
      context.Response.ContentType = "text/plain";

      //Need to retrieve ISession using your NHibernate session provider. In my case it is done in the following way
      ISession session = Portal.Instance.GetCurrentSession();

      DoCacheClearing(session);

      context.Response.Write("Done");
  }

  public void DoCacheClearing(ISession session)
  {
      ISessionFactory factory = session.SessionFactory;

      factory.EvictQueries();

      ICollection types = factory.GetAllClassMetadata().Keys;

      foreach (Type type in types)
          factory.Evict(type);

      foreach (string role in factory.GetAllCollectionMetadata().Keys)
          factory.EvictCollection(role);
  }


  public bool IsReusable { get { return false; } }

}

Capterra Top 20
Capterra Top 20
Project Portfolio Management
May-20
GetApp Category Leaders
GetApp Category Leaders
Project Portfolio Management
Jul-20
GetApp Category Leaders
GetApp Category Leaders
Roadmaps
Jul-20
Software Advice FrontRunners
Software Advice FrontRunners
Product Management
May-20
Software Advice Recommended
Software Advice Recommended
Product Roadmap
Mar-20
Software Advice Customer Support
Software Advice Customer Support
Project Portfolio Management Software
Jun-20

Subscribe to the latest updates

Thank you!

Сheck out latest blog posts: Show all