Module: Lightly::CacheOperations

Included in:
Lightly, Lightly
Defined in:
lib/lightly/cache_operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dirObject



31
32
33
# File 'lib/lightly/cache_operations.rb', line 31

def dir
  @dir ||= 'cache'
end

#hash=(value) ⇒ Object (writeonly)

Sets the attribute hash

Parameters:

  • value

    the value to set the attribute hash to.



6
7
8
# File 'lib/lightly/cache_operations.rb', line 6

def hash=(value)
  @hash = value
end

Instance Method Details

#cached?(key) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/lightly/cache_operations.rb', line 58

def cached?(key)
  path = get_path key
  File.exist?(path) and File.size(path) > 0 and !expired?(path)
end

#clear(key) ⇒ Object



43
44
45
46
# File 'lib/lightly/cache_operations.rb', line 43

def clear(key)
  path = get_path key
  FileUtils.rm path if File.exist? path
end

#disableObject



67
68
69
# File 'lib/lightly/cache_operations.rb', line 67

def disable
  @enabled = false
end

#enableObject



63
64
65
# File 'lib/lightly/cache_operations.rb', line 63

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/lightly/cache_operations.rb', line 39

def enabled?
  @enabled ||= (@enabled.nil? ? true : @enabled)
end

#flushObject



48
49
50
51
# File 'lib/lightly/cache_operations.rb', line 48

def flush
  return false if dir == '/' || dir.empty?
  FileUtils.rm_rf dir
end

#get(key, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/lightly/cache_operations.rb', line 15

def get(key, &block)
  return load key if cached?(key) && enabled?

  content = block.call
  save key, content if content && enabled?
  content
end

#get_path(key) ⇒ Object



71
72
73
74
# File 'lib/lightly/cache_operations.rb', line 71

def get_path(key)
  key = Digest::MD5.hexdigest(key) if hash?
  File.join dir, key
end

#hash?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/lightly/cache_operations.rb', line 35

def hash?
  @hash ||= (@hash.nil? ? true : @hash)
end

#initialize(dir: 'cache', life: '1h', hash: true, enabled: true) ⇒ Object



8
9
10
11
12
13
# File 'lib/lightly/cache_operations.rb', line 8

def initialize(dir: 'cache', life: '1h', hash: true, enabled: true)
  @dir = dir
  @life = life_to_seconds life
  @hash = hash
  @enabled = enabled
end

#lifeObject



23
24
25
# File 'lib/lightly/cache_operations.rb', line 23

def life
  @life ||= 3600
end

#life=(new_life) ⇒ Object



27
28
29
# File 'lib/lightly/cache_operations.rb', line 27

def life=(new_life)
  @life = life_to_seconds new_life
end

#pruneObject



53
54
55
56
# File 'lib/lightly/cache_operations.rb', line 53

def prune
  return false if dir == '/' || dir.empty?
  Dir["#{dir}/*"].each { |file| expired? file }
end

#save(key, content) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/lightly/cache_operations.rb', line 76

def save(key, content)
  FileUtils.mkdir_p dir
  path = get_path key
  File.open path, 'wb' do |f| 
    f.write Marshal.dump content
  end
end