如何无效ASP.NET缓存如果PostgreSQL数据库的数据变化

编程入门 行业动态 更新时间:2024-10-27 03:43:16
本文介绍了如何无效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数据库的数据变化

本文发布于:2023-10-17 13:23:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   数据库   数据   ASP   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!