更新表时触发Guava缓存刷新 [英] Trigger Guava cache refresh when a table is updated

查看:641
本文介绍了更新表时触发Guava缓存刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在使用ClientDAO.getClients()从数据库中获取客户端列表,并使用guava进行缓存,并具有30分钟的刷新时间,如下所示

Presently am fectching the list of clients from db using ClientDAO.getClients() and caching using guava with 30 minutes refresh period as below

private List<String> getClients() {
        final Supplier<List<String>> supplier = () ->  ClientDAO.getClients();
        Suppliers.memoizeWithExpiration(supplier, 30, TimeUnit.MINUTES);
        return supplier.get();
    }

我们有不同的应用程序来更新client表,另一个应用程序是从client表中读取并如上所述进行缓存的,这两个应用程序之间没有Restful或任何类型的api通信.每当数据库中相应的client表被更新而不是在30分钟的特定时间窗口刷新时,是否有任何方法可以触发此缓存更新?

We have different application that updates client table and another application that reads from from the client table and caches as above and there is no Restful or any sort of api communication between these 2 applications. Is there any way to trigger this cache update whenever the corresponding client table in the database is updated instead of refreshing at specific time window 30 minutes?

推荐答案

因此,从这个问题出发,我将假设:

So from the question I will assume:

  • 有两个应用程序AB
  • 有一个客户表-我们称它为table X
  • Atable X
  • 读取并缓存数据
  • B将数据写入table X
  • there are two applications A and B
  • there's a client table - let's call it table X
  • A reads and caches data from table X
  • B writes data to table X

A需要知道B是何时写入table X的,以便A可以更新其缓存的数据.

A needs to know when B has written in table X, so that A can update it's cached data.

如果控制AB,则可以扩展行为-当B写入数据库并且写入成功时, B将事件触发到A (例如调用REST端点),并且A将知道更新其缓存.

If you control A and B, you can extend the behavior - when B writes to the database and the write is successful, B fires an event to A (e.g. invokes a REST endpoint) and A will know to update its cache.

如果您无法控制B,但是可以控制ADB,则可以使用DB作为集成点-这是旧版应用程序的常用方法-DB是应用程序可以集成的唯一地方.

If you can't control B, but you can control A and the DB, you can use the DB as integration point - it's a common approach for legacy apps - the DB is the only place where the applications can integrate.

因此,您可以再次应用与 B相同的概念向A 触发事件,但是这次事件(由触发器启动)存储在,然后使用内部过程从数据库本身进行REST调用.

So again you can apply the same concept of B fires an event to A, but this time the event (initiated by a trigger) is stored in a table in the DB and the REST call is made from the DB itself using an internal procedure.

直接的方法是:

  • B将数据写入table X
  • table X有一个insert trigger,它在事件存储表中执行了一条INSERT语句(我们称之为UPDATE_CACHE_EVENT)
  • B writes data in table X
  • there's an insert trigger for table X, which does a single INSERT statement into event storage table (let's call it UPDATE_CACHE_EVENT)

您的"update_cache_event"现在被解雇(例如保存在该表中),从这一点开始,您还有更多选择:

Your "update_cache_event" is now fired (e.g. saved in that table) and from this point on you have more options:

  • A可以每秒监视此表并在写入新事件时触发其缓存更新

  • A can monitor this table every second and trigger it's cache update when a new event is written

A公开了REST API,该API是从DB调用的-例如在SQL Server中编写一个过程,该过程监视UPDATE_CACHE_EVENT表并调用A的REST API.

A exposes REST API, which is invoked from the DB - e.g. in SQL server write a procedure, that monitors UPDATE_CACHE_EVENT table and calls A's REST API.

一些想法:

  • 请勿使用触发器本身来调用外部API,为此请使用专用过程
  • 触发器会降低性能(例如,每次对table X的写入都会调用事件表的另一个插入),因此在设计解决方案时应考虑到这一点.
  • don't use a trigger itself to call the external API, use a dedicated procedure for that
  • the trigger has performance penalty (e.g. each write to table X will invoke another insert to the event table), so you should consider this when designing your solution.

这篇关于更新表时触发Guava缓存刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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