如何无效ASP.NET缓存如果PostgreSQL数据库的数据变化 [英] How to invalidate ASP.NET cache if data changes in PostgreSql database

查看:120
本文介绍了如何无效ASP.NET缓存如果PostgreSQL数据库的数据变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET/MONO MVC2应用标准的ASP.NET Web缓存用于加快数据库访问:

ASP.NET/MONO MVC2 application standard ASP.NET Web cache is used to speed up database access:

string GetName() {
  // todo: dedect if data has changed and invalidate cache
  var name = (string)HttpContext.Current.Cache["Name"];
  if (name!=null)
    return name;
  name = db.Query("SELECT name from mydata");
  HttpContext.Current.Cache.Insert("Name", name);
  return name;
}

MYDATA可以被其他应用程序更改。
在这种情况下,该方法返回错误的数据。
如何在数据发生变化检测和PostgreSQL数据库在这种情况下,返回新的数据?

mydata can changed by other application. In this case this method returns wrong data. How to detect if data is changed and return fresh data from PostgreSql database in this case ?

这是确定以清除整个Web缓存如果MYDATA已经改变了。

It is OK to clear whole Web cache if mydata has changed.

推荐答案

要做到这一点,最好的办法是有可能与的 收听通知

The best way to do this is likely with LISTEN and NOTIFY.

让你的应用程序保持一个背景工人,到数据库的持久连接。在这方面,发出 LISTEN name_changed ,然后等待通知。如果npgsql支持它,它可能提供一个回调;否则,你将不得不投票表决。

Have your app maintain a background worker with a persistent connection to the DB. In that connection, issue a LISTEN name_changed, then wait for notifications. If npgsql supports it it might offer a callback; otherwise you'll have to poll.

触发器添加到名称表发出通知name_changed

当您的背景工人得到通知就可以刷新缓存。

When your background worker gets the notification it can flush the cache.

您甚至可以使用通知的有效载荷有选择性地只无效更改的条目。

You can even use the NOTIFY payload to invalidate only changed entries selectively.

这篇关于如何无效ASP.NET缓存如果PostgreSQL数据库的数据变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆