简体   繁体   English

在Ruby Gem中缓存,可能不使用Rails

[英]Caching in Ruby Gem, possibly not using Rails

I am rewriting an existing Ruby Gem to include caching. 我正在重写现有的Ruby Gem以包括缓存。 This is for a gem that is relatively commonly used, and accesses a large amount of static data on a web service. 这是相对较常用的gem,它访问Web服务上的大量静态数据。 Currently, I have a small number of gem users doing a large number of accesses to the service that under normal conditions would be swamping / downing the service , and we're going to put the gem up on github for general consumption. 目前,我有少量的gem用户对服务进行大量访问,在正常情况下,它们会淹没/关闭该服务 ,我们将把gem放在github上以供一般使用。

Right now, users can choose between using the rails cache mechanism, a simple disk cache, or no cache. 现在,用户可以选择使用Rails缓存机制,简单磁盘缓存还是不使用缓存。

What is best practice for letting people choose what cache to use like this (being able to use this outside of rails is a priority so i can't just bail to the underlying caching mechanism)? 让人们选择像这样使用哪种缓存的最佳实践是什么(能够在rails之外使用它是一个优先事项,因此我不能仅仅依靠底层的缓存机制)? I'm looking for suggestions/examples for configuration and interface, especially. 我正在寻找有关配置和界面的建议/示例。

Thanks for your suggestions 谢谢你的建议

I wrote a gem called "cachecataz" that has lets any embedder define the caching mechanism/provider they want to use. 我写了一个叫做“ cachecataz”的gem,它可以让任何嵌入程序定义他们要使用的缓存机制/提供程序。 You can easily use the same methodology to allow the users to pick between multiple different caching mechanisms. 您可以轻松地使用相同的方法来允许用户在多种不同的缓存机制之间进行选择。

I chose to define the api as a "Provider" and then an "api" which defines which methods are required for any provider. 我选择将api定义为“提供者”,然后将其定义为任何提供者所需的方法的“ api”。 This is what the "Rails.cache" provider and api look like. 这就是“ Rails.cache”提供程序和api的外观。

Cachecataz.provider = Rails.cache
Cachecataz.api = {:get => :read, :set => :write, :incr => :increment, :exist? => :exist?}

This is a really simple way to have someone choose the Object that responds to the needed methods by the gem writer. 这是一种非常简单的方法,可以让某人选择由gem writer响应所需方法的对象。 Each value in the api hash can either be a symbol or an Object that responds to :call (like Proc or lambda). api哈希中的每个值都可以是符号或响应:call的对象(例如Proc或lambda)。 Then I just use my internal representation in my gem (:get, :set, :incr, :exist?) and lookup the Object/method that needs to be called to execute it at runtime: 然后,我只在gem(:get,:set,:incr,:exist?)中使用内部表示形式,并查找在运行时执行该对象所需的对象/方法:

def make_api_call(method, *args)
  if Cachecataz.api[method].respond_to?(:call)
    Cachecataz.api[method].call(*args)
  else
    Cachecataz.provider.send(Cachecataz.api[method], *args)
  end
end

This is not all the code, but it is close enough to illustrate how quickly you can implement a pluggable caching api in your own gem and support many cache mechanisms/providers. 这还不是全部代码,但足够接近,足以说明您可以在自己的gem中实现可插拔缓存api并支持许多缓存机制/提供程序的速度。 Cachecataz is open source and up on github if you want to look through the code, it is pretty short and documented. Cachecataz是开源的,如果您想查看代码,可以在github上进行访问,它非常简短,并有文档记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM