Parent

Files

Padrino::Cache::Store::File

File based Cache Store

Public Class Methods

new(root) 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")

@api public

# File lib/padrino-cache/store/file.rb, line 20
def initialize(root)
  @root = root
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 91
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 105
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 35
def get(key)
  init
  if ::File.exist?(path_for_key(key))
    contents = ::File.read(path_for_key(key))
    expires_in, body = contents.split("\n", 2)
    expires_in = expires_in.to_i
    if expires_in == -1 or Time.new.to_i < expires_in
      Marshal.load(body) if body
    else
      delete(key)
      nil
    end
  else
    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 66
def set(key, value, opts = nil)
  init
  if opts && opts[:expires_in]
    expires_in = opts[:expires_in].to_i
    expires_in = Time.new.to_i + expires_in if expires_in < EXPIRES_EDGE
  else
    expires_in = -1
  end
  value = Marshal.dump(value) if value
  ::File.open(path_for_key(key), 'w') { |f| f << expires_in.to_s << "\n" << value } if value
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.