使用过期密钥缓存

Jef*_*rod 4 ruby caching ruby-on-rails mashup web-scraping

我正在开发一个mashup站点,并希望限制抓取源站点的数量.我需要的数据基本上只有一位,一个整数,并希望用定义的有效期缓存它.

为了澄清,我只想缓存整数,而不是整个页面源.

是否有红宝石或铁轨功能或宝石已经为我完成了这个?

Kon*_*che 9

就在这里 ActiveSupport::Cache::Store

抽象缓存商店类.有多个缓存存储实现,每个实现都有自己的附加功能.请参阅ActiveSupport :: Cache模块下的类,例如ActiveSupport :: Cache :: MemCacheStore.MemCacheStore是目前大型生产网站最受欢迎的缓存商店.

某些实现可能不支持除fetch,write,read,exist?和delete之类的基本缓存方法之外的所有方法.

ActiveSupport :: Cache :: Store可以存储任何可序列化的Ruby对象.

http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html

cache = ActiveSupport::Cache::MemoryStore.new
cache.read('Chicago')   # => nil 
cache.write('Chicago', 2707000)
cache.read('Chicago')   # => 2707000
Run Code Online (Sandbox Code Playgroud)

关于到期时间,这可以通过将时间作为初始化参数来完成

cache = ActiveSupport::Cache::MemoryStore.new(expires_in: 5.minutes)
Run Code Online (Sandbox Code Playgroud)

如果要缓存具有不同到期时间的值,也可以在将值写入缓存时设置此值

cache.write(key, value, expires_in: 1.minute) # Set a lower value for one entry
Run Code Online (Sandbox Code Playgroud)