Files

Padrino::Cache::Store::File

File based Cache Store

Public Class Methods

new(root, options={}) click to toggle source

Initialize File store with File root

@param [String] root

path to cache file

@example

Padrino.cache = Padrino::Cache::Store::File.new("path/to")
# or from your app
set :cache, Padrino::Cache::Store::File.new("path/to")
# you can provide a marshal parser (to store ruby objects)
set :cache, Padrino::Cache::Store::File.new("path/to", :parser => :marshal)

@api public

# File lib/padrino-cache/store/file.rb, line 22
def initialize(root, options={})
  @root = root
  super(options)
end

Public Instance Methods

delete(key) click to toggle source

Delete the value for a given key

@param [String] key

cache key

@param value

value of cache key

@example

# with: MyApp.cache.set('records', records)
MyApp.cache.delete('records')

@api public

# File lib/padrino-cache/store/file.rb, line 88
def delete(key)
  init
  Array(key).each { |k| FileUtils.rm_rf(path_for_key(k)) }
end
flush() click to toggle source

Reinitialize your cache

@example

# with: MyApp.cache.set('records', records)
MyApp.cache.flush
MyApp.cache.get('records') # => nil

@api public

# File lib/padrino-cache/store/file.rb, line 102
def flush
  FileUtils.rm_rf(@root)
end
get(key) click to toggle source

Return the a value for the given key

@param [String] key

cache key

@example

# with MyApp.cache.set('records', records)
MyApp.cache.get('records')

@api public

# File lib/padrino-cache/store/file.rb, line 38
def get(key)
  init
  if ::File.exist?(path_for_key(key))
    read_method = ::File.respond_to?(:binread) ? :binread : :read
    contents    = ::File.send(read_method, path_for_key(key))
    expiry, body = contents.split("\n", 2)
    if now_before? expiry
      parser.decode(body) if body
    else # expire the key
      delete(key)
      nil
    end
  else # key can't be found
    nil
  end
end
set(key, value, opts = nil) click to toggle source

Set the value for a given key and optionally with an expire time Default expiry time is 86400.

@param [String] key

cache key

@param value

value of cache key

@example

MyApp.cache.set('records', records)
MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds

@api public

# File lib/padrino-cache/store/file.rb, line 69
def set(key, value, opts = nil)
  init
  value = parser.encode(value) if value
  ::File.open(path_for_key(key), 'wb') { |f| f << get_expiry(opts).to_s << "\n" << value } if value
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.