Class: Diskcached

Inherits:
Object
  • Object
show all
Defined in:
lib/diskcached.rb

Overview

Author:

Defined Under Namespace

Classes: NotFound

Constant Summary collapse

VERSION =

version for gem

'1.1.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = "/tmp/cache", timeout = 600, autogc = true) ⇒ Diskcached

initialize object

  • set #store to passed or default (‘/tmp/cache’)

  • set #timeout to passed or default (‘600’)

  • set #autogc to passed or default (‘false’)

  • run #ensure_store_directory



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#gc_autoObject (readonly)

should auto_garbage_collect



16
17
18
# File 'lib/diskcached.rb', line 16

def gc_auto
  @gc_auto
end

#gc_lastObject (readonly)

time of last #garbage_collect



13
14
15
# File 'lib/diskcached.rb', line 13

def gc_last
  @gc_last
end

#gc_timeObject (readonly)

how often to auto_garbage_collect



19
20
21
# File 'lib/diskcached.rb', line 19

def gc_time
  @gc_time
end

#storeObject (readonly)

disk location for cache store



7
8
9
# File 'lib/diskcached.rb', line 7

def store
  @store
end

#timeoutObject

cache timeout



10
11
12
# File 'lib/diskcached.rb', line 10

def timeout
  @timeout
end

Instance Method Details

#cache(key) ⇒ Object

create and read cache with ‘key’

  • creates cache if it doesn’t exist

  • reads cache if it exists



89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object

returns path to cache file with ‘key’



145
146
147
# File 'lib/diskcached.rb', line 145

def cache_file key
  File.join( store, key+".cache" )
end

#delete(key) ⇒ Object

expire cache with ‘key’



56
57
58
# File 'lib/diskcached.rb', line 56

def delete key
  File.delete( cache_file(key) ) if File.exists?( cache_file(key) )
end

#expired?(key) ⇒ Boolean

return true if cache with ‘key’ is expired

Returns:

  • (Boolean)


49
50
51
52
53
# 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

#flushObject

expire (delete) all caches in #store directory



61
62
63
64
65
# File 'lib/diskcached.rb', line 61

def flush
  Dir[ File.join( store, '*.cache' ) ].each do |file|
    File.delete(file)
  end
end

#flush_expiredObject

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



69
70
71
72
73
# 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!Object

flash expired caches, ingoring when garbage collection was last run



77
78
79
80
81
82
83
84
# 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) ⇒ Object

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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

#set(key, value) ⇒ Object Also known as: add, replace

set cache with ‘key’

  • run #auto_garbage_collect

  • creates cache if it doesn’t exist



105
106
107
108
109
110
111
112
113
114
# 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