跳至内容 跳至搜索

文件缓存存储

一种缓存存储实现,将所有内容存储在文件系统中。

方法
C
D
I
N
S

常量

DIR_FORMATTER = "%03X"
 
FILENAME_MAX_SIZE = 226
 
FILEPATH_MAX_SIZE = 900
 
GITKEEP_FILES = [".gitkeep", ".keep"].freeze
 

属性

[R] cache_path

类公共方法

new(cache_path, **options)

# File activesupport/lib/active_support/cache/file_store.rb, line 20
def initialize(cache_path, **options)
  super(options)
  @cache_path = cache_path.to_s
end

supports_cache_versioning?()

宣传缓存版本支持。

# File activesupport/lib/active_support/cache/file_store.rb, line 26
def self.supports_cache_versioning?
  true
end

实例公共方法

cleanup(options = nil)

抢先迭代所有存储的键,并移除已过期的键。

# File activesupport/lib/active_support/cache/file_store.rb, line 40
def cleanup(options = nil)
  options = merged_options(options)
  search_dir(cache_path) do |fname|
    entry = read_entry(fname, **options)
    delete_entry(fname, **options) if entry && entry.expired?
  end
end

clear(options = nil)

删除缓存中的所有项目。在这种情况下,它会删除指定文件存储目录中的所有条目,除了 .keep 或 .gitkeep。使用 FileStore 时,请注意配置文件中指定的目录,因为该目录中的所有内容都将被删除。

# File activesupport/lib/active_support/cache/file_store.rb, line 33
def clear(options = nil)
  root_dirs = (Dir.children(cache_path) - GITKEEP_FILES)
  FileUtils.rm_r(root_dirs.collect { |f| File.join(cache_path, f) })
rescue Errno::ENOENT, Errno::ENOTEMPTY
end

decrement(name, amount = 1, options = nil)

递减缓存的整数的值。返回更新后的值。

如果键未设置,它将被设置为 -amount

cache.decrement("foo") # => -1

要设置特定值,请调用 write

cache.write("baz", 5)
cache.decrement("baz") # => 4
# File activesupport/lib/active_support/cache/file_store.rb, line 80
def decrement(name, amount = 1, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  instrument(:decrement, key, amount: amount) do
    modify_value(name, -amount, options)
  end
end

delete_matched(matcher, options = nil)

# File activesupport/lib/active_support/cache/file_store.rb, line 89
def delete_matched(matcher, options = nil)
  options = merged_options(options)
  matcher = key_matcher(matcher, options)

  instrument(:delete_matched, matcher.inspect) do
    search_dir(cache_path) do |path|
      key = file_path_key(path)
      delete_entry(path, **options) if key.match(matcher)
    end
  end
end

increment(name, amount = 1, options = nil)

递增缓存的整数的值。返回更新后的值。

如果键未设置,它将从 0 开始。

cache.increment("foo") # => 1
cache.increment("bar", 100) # => 100

要设置特定值,请调用 write

cache.write("baz", 5)
cache.increment("baz") # => 6
# File activesupport/lib/active_support/cache/file_store.rb, line 60
def increment(name, amount = 1, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  instrument(:increment, key, amount: amount) do
    modify_value(name, amount, options)
  end
end