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数据库的数据变化
发布评论