我应该如何在Spring中实现一个缓存对象/系统? [英] How should I implement a cache object/system in Spring?

查看:93
本文介绍了我应该如何在Spring中实现一个缓存对象/系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Spring应用程序,该应用程序有数百个用户,但是有100-1百万条数据作为响应发送给每个用户.因此,性能对我们来说是一个大问题.我们利用Java,JSP,jQuery,HTML和CSS.对于我正在做的工作,我正在构建一个通知系统.例如,如果用户的商品过期了,那么我们会向该用户发送通知.但是,问题是:

I am working on an Spring application that has hundreds of users but has 100-1 million pieces of data that is being sent as a response to each individual user. As such, performance is a big issue for us. We utilise Java, JSP, jQuery, HTML and CSS. For the bit I am working on, I am building a notification system. For example, if a user has an item that has past their expiry date then we send a notification to the user. However, the problems are:

  • 我正在使用jQuery/AJAX每分钟轮询服务器
  • 每个查询非常昂贵,因为我们正在数据库中为每个用户每分钟搜索数十万个数据.而且,有数百个用户.
  • 我们不在检查上次修改时间,而是在检查此到期日期字段是否在当前时间之前.

我现在的想法是使用一个线程,该线程将连续检查数据库中是否有新更新的项目,如果有新项目,我们将更新缓存对象的表示形式.用户将从缓存中检索数据.

The idea I have right now is to use a thread that will continuously check the database for newly updated items and if there are new items we update a representation of a cache object. The users will retrieve data from the cache.

我应该如何用Spring来实现呢?我应该使用什么数据结构?我应该为线程和缓存对象使用什么?我应该为此使用WeakHashMap吗?

How should I implement this with Spring? What data structures should I use? What should I use for the thread and for the cache object? Should I use a WeakHashMap for this?

注意:我们的应用程序不支持注释驱动的mvc.

Note: Our application does not support annotation driven mvc.

推荐答案

Spring在3.x RELEASE中引入了Cache的抽象.您可以在Spring官方文档(由于某种原因该站点今天已关闭:)中,或在此信息中,了解有关它的信息.

Spring introduced abstraction for Cache in 3.x RELEASE. You can read about it in official Spring documentation (the site is down today for some reason :)), or at this post for example.

http://java.dzone.com/articles/spring-cache- abstraction-0

使用这种抽象,启用缓存所需要做的就是向服务中添加一些注释,例如

With this abstraction, all you need to do to enable cache is to add some annotations to your services, like

为缓存增加价值

@Cacheable("customers")
public Customer findCustomer(long customerId) {...}

要删除缓存中的值

@CacheEvict(value="customer", allEntries = true)
public void removeAllCustomers(long customerId) {...}

并启用可缓存的spring配置.春季魔术AOP负责其余的工作. 作为Spring的所有组件,您可以使用所需的任何实现,并且开箱即用地支持许多实现.而且您不需要为此更改代码分配,只需根据需要添加注释:)

And enable cacheable spring configuration. Spring magic AOP takes care of the rest. And as everything with Spring you can use any implementation you want, and many implementation are supported out of the box. And you don't need to change allot of code for this, just add annotations, as needed:)

除了Spring本机支持之外,还有Guava缓存

Besides Spring native support, there is Guava Cache

https://code.google.com/p/guava-libraries/wiki/CachesExplained

您可以选择所需的内容,并通过指定高速缓存密钥生存期等方法来满足您的要求,这样它将在特定时间后从高速缓存中删除,并在下一次调用时重新计算.

You can choose what you want, and implement your requirement by for example specifying cache key life timeout, so it would be removed from cache after specific time, and recalculated on next call.

这篇关于我应该如何在Spring中实现一个缓存对象/系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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