Class: Lux::Cache
- Inherits:
-
Object
show all
- Defined in:
- lib/lux/cache/cache.rb,
lib/lux/cache/lib/null_server.rb,
lib/lux/cache/lib/memory_server.rb,
lib/lux/cache/lib/sqlite_server.rb,
lib/lux/cache/lib/memcached_server.rb
Defined Under Namespace
Classes: MemcachedServer, MemoryServer, NullServer, SqliteServer
Instance Method Summary
collapse
-
#[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
-
#clear ⇒ Object
-
#delete(key, data = nil) ⇒ Object
-
#fetch(key, opts = {}) ⇒ Object
-
#generate_key(*data) ⇒ Object
-
#initialize(server_name = nil) ⇒ Cache
constructor
-
#is_available? ⇒ Boolean
-
#lock(key, time) ⇒ Object
lock execution of a block for some time and allow only once instance running in time slot give some block 3 seconds to run, if another instance executes same block after 1 second, if will wait 2 seconds till it wil continue Lux.cache.lock ‘some-key’, 3 do …
-
#log_get(name) ⇒ Object
-
#read(key) ⇒ Object
(also: #get)
-
#read_multi(*args) ⇒ Object
(also: #get_multi)
-
#server ⇒ Object
-
#server=(name) ⇒ Object
sert cache server Lux.cache.server = :memory Lux.cache.server = :memcached Lux.cache.server = Dalli::Client.new(‘localhost:11211’, { :namespace=>Digest::MD5.hexdigest(__FILE__), :compress => true, :expires_in => 1.hour }).
-
#write(key, data, ttl = nil) ⇒ Object
(also: #set)
Constructor Details
#initialize(server_name = nil) ⇒ Cache
Returns a new instance of Cache.
5
6
7
|
# File 'lib/lux/cache/cache.rb', line 5
def initialize server_name = nil
self.server= server_name || :memory
end
|
Instance Method Details
146
147
148
|
# File 'lib/lux/cache/cache.rb', line 146
def [] key
@server.get key.to_s
end
|
#[]=(key, value) ⇒ Object
141
142
143
144
|
# File 'lib/lux/cache/cache.rb', line 141
def []= key, value
@server.set key.to_s, value
value
end
|
107
108
109
|
# File 'lib/lux/cache/cache.rb', line 107
def clear
@server.clear
end
|
#delete(key, data = nil) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/lux/cache/cache.rb', line 49
def delete key, data=nil
key = generate_key key
Lux.log do
if Lux.config[:show_cache_log]
%[ Cache.delete "#{key}", at: #{Lux.app_caller}].yellow
end
end
@server.delete(key)
end
|
#fetch(key, opts = {}) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/lux/cache/cache.rb', line 61
def fetch key, opts={}
key = generate_key key
opts = { ttl: opts } unless opts.is_a?(Hash)
opts = OPTS.new **opts
return yield if opts.if.is_a?(FalseClass)
opts.ttl = opts.ttl.to_i if opts.ttl
opts.force ||= Lux.current.try(:no_cache?) unless opts.force.class == FalseClass
@server.delete key if opts.force
log_key_name = "Cache.fetch.get #{opts.compact.to_jsonc}:#{key.trim(30)}"
log_get log_key_name
data = @server.fetch key, opts.ttl do
opts.speed = Lux.speed { data = yield }
Lux.log " #{log_key_name}, at: #{Lux.app_caller}".yellow
Marshal.dump data
end
Marshal.load(data).tap do |out|
if opts.delete_if_empty && out.empty?
@server.delete key
end
end
end
|
#generate_key(*data) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/lux/cache/cache.rb', line 116
def generate_key *data
if data[0].class == String && !data[1]
return data[0]
end
keys = []
for el in [data].flatten
keys.push el.class.to_s
keys.push el.id if el.respond_to?(:id)
if el.respond_to?(:updated_at)
keys.push el.updated_at.to_f
elsif el.respond_to?(:id)
keys.push el.id
else
keys.push el.to_s
end
end
key = keys.join('-')
Crypt.sha1(key)
end
|
#is_available? ⇒ Boolean
111
112
113
114
|
# File 'lib/lux/cache/cache.rb', line 111
def is_available?
set('lux-test', 9)
get('lux-test') == 9
end
|
#lock(key, time) ⇒ Object
lock execution of a block for some time and allow only once instance running in time slot give some block 3 seconds to run, if another instance executes same block after 1 second, if will wait 2 seconds till it wil continue Lux.cache.lock ‘some-key’, 3 do …
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/lux/cache/cache.rb', line 93
def lock key, time
key = "syslock-#{key}"
cache_time = Lux.cache.get key
if cache_time && cache_time > (Time.monotonic - time)
diff = time - (Time.monotonic - cache_time)
sleep diff.abs
else
Lux.cache.set(key, Time.monotonic, time)
end
yield
end
|
#log_get(name) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/lux/cache/cache.rb', line 150
def log_get name
if Lux.env.screen_log?
if Lux.current.params[:lux_show_cache_get]
Lux.config[:show_cache_log] = true
end
if Lux.config[:show_cache_log]
Lux.log " Cache.get #{name}, at: #{Lux.app_caller}".green
else
if Lux.current.once(:show_cache_log)
Lux.log " Cache.get info is suppressed: enable? -> #{Lux.current.nav.base}#{Lux.current.request.path}?lux_show_cache_get=true".green
end
end
end
end
|
#read(key) ⇒ Object
Also known as:
get
27
28
29
30
31
32
|
# File 'lib/lux/cache/cache.rb', line 27
def read key
return nil if (Lux.current.no_cache? rescue false)
key = generate_key key
log_get "Cache.read #{key}"
@server.get(key)
end
|
#read_multi(*args) ⇒ Object
Also known as:
get_multi
35
36
37
|
# File 'lib/lux/cache/cache.rb', line 35
def read_multi *args
@server.get_multi(*args)
end
|
23
24
25
|
# File 'lib/lux/cache/cache.rb', line 23
def server
@server
end
|
#server=(name) ⇒ Object
sert cache server Lux.cache.server = :memory Lux.cache.server = :memcached Lux.cache.server = Dalli::Client.new(‘localhost:11211’, { :namespace=>Digest::MD5.hexdigest(__FILE__), :compress => true, :expires_in => 1.hour })
13
14
15
16
17
18
19
20
21
|
# File 'lib/lux/cache/cache.rb', line 13
def server= name
@server =
if name.is_a?(Symbol)
require_relative 'lib/%s_server' % name
@server = ('lux/cache/%s_server' % name).classify.constantize.new
else
name
end
end
|
#write(key, data, ttl = nil) ⇒ Object
Also known as:
set
40
41
42
43
44
45
46
|
# File 'lib/lux/cache/cache.rb', line 40
def write key, data, ttl = nil
ttl = ttl[:ttl] || ttl[:expires_at] if ttl.class == Hash
ttl = ttl.to_i if ttl
key = generate_key key
Lux.log %[ Cache.write "#{key}", at: #{Lux.app_caller}].yellow
@server.set(key, data, ttl)
end
|