如何在 Ruby on Rails 上列出内存缓存存储中的键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9602286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:59:00  来源:igfitidea点击:

How can I list the keys in the in-memory cache store on Ruby on Rails?

ruby-on-rails

提问by Nerian

I am using Rails 3.

我正在使用 Rails 3。

How can I list the keys in the in-memory cache store on Ruby on Rails?

如何在 Ruby on Rails 上列出内存缓存存储中的键?

采纳答案by Jordan Running

ActiveSupport::Cache::MemoryStoredoesn't provide a way to access the store's keys directly (and neither does its parent class ActiveSupport::Cache::Store).

ActiveSupport::Cache::MemoryStore不提供直接访问存储键的方法(其父类ActiveSupport::Cache::Store也不提供)。

Internally MemoryStore keeps everything in a Hash called @data, however, so you could monkey-patch or subclass it to get the keys, e.g.:

@data但是,在内部 MemoryStore 将所有内容保存在名为 的 Hash中,因此您可以对它进行猴子补丁或子类化以获取密钥,例如:

class InspectableMemoryStore < ActiveSupport::Cache::MemoryStore
  def keys
    @data.keys
  end
end

ActionController::Base.cache_store = InspectableMemoryStore.new

Rails.cache.keys # => [ "foo", ... ]

This comes with the usual caveat, however: MemoryStore's internal implementation may change at any time and @datamay disappear or be changed to something that doesn't respond_to? :keys. A smarter implementation might be to override the writeand deletemethods (since, as part of the public API, they're unlikely to change unexpectedly) to keep your own list of keys, e.g.:

然而,这带有通常的警告:MemoryStore 的内部实现可能随时更改,并且@data可能会消失或更改为不存在的内容respond_to? :keys。更聪明的实现可能是覆盖writedelete方法(因为作为公共 API 的一部分,它们不太可能意外更改)以保留您自己的密钥列表,例如:

class InspectableMemoryStore < ActiveSupport::Cache::MemoryStore
  def write *args
    super

    @inspectable_keys[ args[0] ] = true
  end

  def delete *args
    super

    @inspectable_keys.delete args[0]
  end

  def keys
    @inspectable_keys.keys
  end
end

This is a very naive implementation, and of course keeping the keys in an additional structure takes up some memory, but you get the gist.

这是一个非常幼稚的实现,当然,将键保存在一个额外的结构中会占用一些内存,但你明白了要点。

回答by Micha? Dulat

Rails.cache.instance_variable_get(:@data).keys

回答by Maragues

If you don't need to access the keys dynamically, an easier approach is locating the directory where the cache is stored. A file is created for each entry.

如果您不需要动态访问密钥,更简单的方法是定位存储缓存的目录。为每个条目创建一个文件。

In my case this was at "APP_ROOT/tmp/cache", but you can easily locate it by going to rails console and typing

在我的情况下,这是在“APP_ROOT/tmp/cache”,但您可以通过转到 rails 控制台并键入轻松找到它

1.8.7 :030 >   Rails.cache.clear
 => ["path_to_rails_app/tmp/cache/6D5"]

回答by TorvaldsDB

Fetch all keys:

获取所有密钥:

Rails.cache.data.keys

Read the specific key

读取特定键

Rails.cache.read("cache_key")

Delete one key from keys:

从键中删除一个键:

Rails.cache.delete("cache_key")

Flush out the keys

冲出钥匙

Rails.cache.clear