class Diskcached

@author Joshua P. Mervine <joshua@mervine.net>

Constants

VERSION

version for gem

Attributes

gc_auto[R]

should auto_garbage_collect

gc_last[R]

time of last garbage_collect

gc_time[R]

how often to auto_garbage_collect

store[R]

disk location for cache store

timeout[R]

cache timeout

Public Class Methods

new(store="/tmp/cache", timeout=600, autogc=true) click to toggle source

initialize object

# File lib/diskcached.rb, line 26
def initialize store="/tmp/cache", timeout=600, autogc=true
  @store   = store
  @timeout = timeout

  @gc_last, @gc_time = nil

  # true or false, this will be ignored if @gc_last and @gc_time
  # are nil
  @gc_auto = autogc

  unless timeout.nil?
    send(:timeout=, timeout)
  end

  ensure_store_directory
end

Public Instance Methods

add(key, value)
Alias for: set
cache(key) { || ... } click to toggle source

create and read cache with 'key'

  • creates cache if it doesn't exist

  • reads cache if it exists

# File lib/diskcached.rb, line 89
def cache key
  begin
    if expired?(key)
      content = Proc.new { yield }.call
      set( key, content )
    end
    content ||= get( key )
    return content
  rescue LocalJumpError
    return nil
  end
end
cache_file(key) click to toggle source

returns path to cache file with 'key'

# File lib/diskcached.rb, line 145
def cache_file key
  File.join( store, key+".cache" )
end
delete(key) click to toggle source

expire cache with 'key'

# File lib/diskcached.rb, line 56
def delete key
  File.delete( cache_file(key) ) if File.exists?( cache_file(key) )
end
expired?(key) click to toggle source

return true if cache with 'key' is expired

# File lib/diskcached.rb, line 49
def expired? key
  return false if timeout.nil?
  mtime = read_cache_mtime(key)
  return (mtime.nil? || mtime+timeout <= Time.now)
end
flush() click to toggle source

expire (delete) all caches in store directory

# File lib/diskcached.rb, line 61
def flush
  Dir[ File.join( store, '*.cache' ) ].each do |file|
    File.delete(file)
  end
end
flush_expired() click to toggle source

flush expired caches if garbage collection hasn't been run recently

# File lib/diskcached.rb, line 69
def flush_expired
  if gc_last && gc_time && gc_last+gc_time <= Time.now
    flush_expired!
  end
end
flush_expired!() click to toggle source

flash expired caches, ingoring when garbage collection was last run

# File lib/diskcached.rb, line 77
def flush_expired!
  Dir[ File.join( store, "*.cache" ) ].each do |f|
    if (File.mtime(f)+timeout) <= Time.now
      File.delete(f)
    end
  end
  @gc_last = Time.now
end
get(key) click to toggle source

get cache with 'key'

  • reads cache if it exists and isn't expired or raises Diskcache::NotFound

  • if 'key' is an Array returns only keys which exist and aren't expired, it raises Diskcache::NotFound if none are available

# File lib/diskcached.rb, line 124
def get key
  if key.is_a? Array
    ret = {}
    key.each do |k|
      ret[k] = Marshal::load(read_cache_file(k)) unless expired?(k)
    end

    raise if ret.empty?
    return ret
  end

  raise if expired?(key)

  return Marshal::load(read_cache_file(key))
rescue
  raise Diskcached::NotFound
ensure
  flush_expired if gc_auto
end
replace(key, value)
Alias for: set
set(key, value) click to toggle source

set cache with 'key'

  • run auto_garbage_collect

  • creates cache if it doesn't exist

# File lib/diskcached.rb, line 105
def set key, value
  begin
    write_cache_file( key, Marshal::dump(value) )
    flush_expired if gc_auto
    return true
  rescue
    flush_expired if gc_auto
    return false
  end
end
Also aliased as: add, replace
timeout=(t) click to toggle source
# File lib/diskcached.rb, line 43
def timeout=(t)
  @gc_last = Time.now
  @gc_time = t
end

Private Instance Methods

ensure_store_directory() click to toggle source

creates store directory if it doesn't exist

# File lib/diskcached.rb, line 176
def ensure_store_directory
  FileUtils.mkpath( store ) unless File.directory?( store )
end
read_cache_file(key) click to toggle source

reads the actual cache file

# File lib/diskcached.rb, line 160
def read_cache_file key
  f = File.open( cache_file(key), "r" )
  f.flock(File::LOCK_SH)
  out = f.read
  f.close
  return out
end
read_cache_mtime(key) click to toggle source

returns mtime of cache file or nil if file doesn't exist

# File lib/diskcached.rb, line 170
def read_cache_mtime key
  return nil unless File.exists?(cache_file(key))
  File.mtime( cache_file(key) )
end
write_cache_file(key, content) click to toggle source

creates the actual cache file

# File lib/diskcached.rb, line 151
def write_cache_file key, content
  f = File.open( cache_file(key), "w+" )
  f.flock(File::LOCK_EX)
  f.write( content )
  f.close
  return content
end