WCF 数据服务 +过期数据(计时器?)
我想通过 WCF 数据服务(Singlton)公开一些端点,该服务将维护用于响应单个请求的数据集合。
理想情况下,我希望能够在一段时间后使给定请求的内存中保存的数据过期(删除)。
存储的数据将用于构建(仅部分地,因此开箱即用的缓存不可行)结果集以返回给客户端。数据将是来自 API 的对象,并且必须保存在内存中,而不是持久保存到存储中。
我正在寻找触发“清除”过程以检查过期数据的方法。在 ctor 中启动计时器似乎是个坏主意。它可以针对每个请求运行(启用单并发),但这似乎过多,并且当没有更多请求时可能会留下数据?
对这个问题的任何想法表示赞赏。
I would like to expose a few endpoints via a WCF data service (Singlton) which will maintain a collection of data used to respond to individual requests.
Ideally I would like to be able to expire (delete) the data held in memory for a given request after a period of time.
The stored data would be used to build (partially only, so out of the box caching is not ok) a result set to return to the client. The data will be objects from an API and must be kept in memory, not peristed to storage.
I'm looking for ways to trigger the 'purge' process to check for expired data. Kicking off a timer in the ctor seems like a bad idea. It could be run for every request (single concurrency in enabled) but this seems excessive, and would potentially leave data hanging around when there are not more requests?
Any thoughts at all on the issue appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要某种计时器来定期运行清理过程。您可以根据请求触发,但这并不可取,因为您当然不应该阻止可能需要长时间运行的清理过程的请求,并且因为请求之间可能有很长的时间间隔,这意味着请求可能会处理超出其生命周期的数据。
一种选择是通过使读取过滤掉超出其生命周期的数据来使清理过程不再重要。例如,您可以使用内存数据库,例如 SQL Compact Edition 或 Sqlite。缓存的数据可以有一个时间戳列,然后读入缓存可以查询并始终按不早于 X 的时间戳进行过滤。这样做的目的是使得清理的发生并不重要,但是相反,对内存压力的优化确实应该发生。 Sql 只是为您提供了按时间戳进行过滤的简单机制。您可以对自己的内存数据结构执行相同的操作。
就清理过程而言,您需要某种计时器或其他东西来启动它运行。在 proc 服务中启动 WCF 的进程还可以启动一个计时器并定期调用缓存来清理它。如果在清理过程中调用清理调用,它就会返回。如果您将清理设置为不重要(如上所述),并且清理过程在运行时被忽略,那么每个请求也可能会踢掉它。
You need some sort of timer to run the cleanup process on a regular interval. You could trigger on request but that's not advisable because you certainly shouldn't block requests on what could be a long running cleanup process and because you could potentially have long periods of times between requests means the requests could be working off data that's beyond it's lifetime.
One option is to not make the cleanup process not critical by making the reads filter data out beyond it's lifetime. For example, you could use an in memory database like SQL Compact Edition or Sqlite. The cached data could have a timestamp column on it and then reads into the cache could query and always filter by timestamp not older than X. What that does is it makes it not critical for the cleanup to happen but instead an optimization for memory pressure that really should happen. Sql just gives you easy mechanisms to filter by timestamp. You could do the same with your own in memory data structures.
As far as the cleanup process goes, you need some sort of timer or something to kick it to run. The process the starts the WCF in proc service could also start a timer and call into the cache on a periodic basic to clean it up. If a cleanup call gets called while its cleaning up, it would just return. If you make the cleanup not critical (like outlined above) and the cleanup process is ignored if running, then each request could potentially kick it as well.
最终重新设计并在 Windows 服务中托管相关服务组件,并使用系统计时器来清除所需的数据。
Ended up redesigning and hosting the relevant service component in a windows service with a system timer to purge required data.