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



33
34
35
# File 'lib/lightly/cache_operations.rb', line 33

def dir
  @dir ||= 'cache'
end

#hash=(value) ⇒ Object (writeonly)

Sets the attribute hash

Parameters:

  • value

    the value to set the attribute hash to.



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

def hash=(value)
  @hash = value
end

#permissionsObject

Returns the value of attribute permissions.



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

def permissions
  @permissions
end

Instance Method Details

#cached?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#clear(key) ⇒ Object



45
46
47
48
# File 'lib/lightly/cache_operations.rb', line 45

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

#disableObject



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

def disable
  @enabled = false
end

#enableObject



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

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/lightly/cache_operations.rb', line 41

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

#flushObject



50
51
52
53
54
# File 'lib/lightly/cache_operations.rb', line 50

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

  FileUtils.rm_rf dir
end

#get(key) ⇒ Object



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

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

  content = yield
  save key, content if content && enabled?
  content
end

#get_path(key) ⇒ Object



75
76
77
78
# File 'lib/lightly/cache_operations.rb', line 75

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

#hash?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lightly/cache_operations.rb', line 37

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

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



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

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

#lifeObject



25
26
27
# File 'lib/lightly/cache_operations.rb', line 25

def life
  @life ||= 3600
end

#life=(new_life) ⇒ Object



29
30
31
# File 'lib/lightly/cache_operations.rb', line 29

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

#pruneObject



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

def prune
  return false if dir == '/' || dir.empty?

  Dir["#{dir}/*"].each { |file| expired? file }
end

#save(key, content) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/lightly/cache_operations.rb', line 80

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