ruby-on-rails 检查Rails缓存的大小?

kknvjkwl  于 2023-11-20  发布在  Ruby
关注(0)|答案(3)|浏览(149)

有办法检查Rails缓存的大小吗?
标签:Rails.cache.size => 390 MB
我假设数据存储之间存在一些细微的差异,但现在我甚至不确定如何开始检查缓存占用的磁盘空间。

xn1cxnb4

xn1cxnb41#

这完全取决于你的缓存存储和你使用的后端。
这是我的heroku示例运行memcachier的一个例子:

Rails.cache.stats
# => {"xxx.memcachier.com:11211"=>{"curr_items"=>"278", "bytes"=>"3423104", "evictions"=>"0", "total_items"=>"7373", "curr_connections"=>"7", "total_connections"=>"97", "cmd_get"=>"141674", "cmd_set"=>"7373", "cmd_delete"=>"350", "cmd_flush"=>"6", "get_hits"=>"63716", "get_misses"=>"77958", "delete_hits"=>"162", "delete_misses"=>"188", "incr_hits"=>"0", "incr_misses"=>"0", "decr_hits"=>"0", "decr_misses"=>"0"}}

字符串
FileStore没有这样的方法:

Rails.cache.stats
# => NoMethodError: undefined method `stats' for #<ActiveSupport::Cache::FileStore:0x007ff1cbe905b0>


当在本地运行memcached时,我会得到一个不同的结果集:

Rails.cache.stats
# => {"127.0.0.1:11211"=>{"pid"=>"327", "uptime"=>"517931", "time"=>"1392163858", "version"=>"1.4.16", "libevent"=>"2.0.21-stable", "pointer_size"=>"64", "rusage_user"=>"2.257386", "rusage_system"=>"4.345445", "curr_connections"=>"15", "total_connections"=>"16", "connection_structures"=>"16", "reserved_fds"=>"20", "cmd_get"=>"0", "cmd_set"=>"0", "cmd_flush"=>"0", "cmd_touch"=>"0", "get_hits"=>"0", "get_misses"=>"0", "delete_misses"=>"0", "delete_hits"=>"0", "incr_misses"=>"0", "incr_hits"=>"0", "decr_misses"=>"0", "decr_hits"=>"0", "cas_misses"=>"0", "cas_hits"=>"0", "cas_badval"=>"0", "touch_hits"=>"0", "touch_misses"=>"0", "auth_cmds"=>"0", "auth_errors"=>"0", "bytes_read"=>"48", "bytes_written"=>"30", "limit_maxbytes"=>"67108864", "accepting_conns"=>"1", "listen_disabled_num"=>"0", "threads"=>"4", "conn_yields"=>"0", "hash_power_level"=>"16", "hash_bytes"=>"524288", "hash_is_expanding"=>"0", "malloc_fails"=>"0", "bytes"=>"0", "curr_items"=>"0", "total_items"=>"0", "expired_unfetched"=>"0", "evicted_unfetched"=>"0", "evictions"=>"0", "reclaimed"=>"0"}}

o8x7eapl

o8x7eapl2#

除了@phoet的答案,对于Redis缓存,你可以使用以下方法来获得人类可读的格式:

Rails.cache.stats["used_memory_human"] #=> 178.32M

字符串
其中used_memory_human实际上可以是在redis服务器上运行INFO命令时返回的任何键。

goucqfw6

goucqfw63#

如果你使用的是memory_store,你可以通过修改MemoryStore类来获取使用的缓存大小。
config/initializers/inspectable_memory_store.rb

module ActiveSupport
  module Cache
    class MemoryStore

      include ActionView::Helpers::NumberHelper

      def size
        number_to_human_size(@cache_size)
      end
    end
  end
end

字符串
现在,Rails.cache.size将给予您已使用的缓存大小

相关问题