forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.rb
67 lines (45 loc) · 1022 Bytes
/
cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Cache
=begin
delete a cache
Cache.delete('some_key')
=end
def self.delete(key)
Rails.cache.delete(key.to_s)
end
=begin
write a cache
Cache.write(
'some_key',
{ some: { data: { 'structure' } } },
{ expires_in: 24.hours, # optional, default 7 days }
)
=end
def self.write(key, data, params = {})
if !params[:expires_in]
params[:expires_in] = 7.days
end
# in certain cases, caches are deleted by other thread at same
# time, just log it
begin
Rails.cache.write(key.to_s, data, params)
rescue => e
Rails.logger.error "Can't write cache #{key}: #{e.inspect}"
end
end
=begin
get a cache
value = Cache.get('some_key')
=end
def self.get(key)
Rails.cache.read(key.to_s)
end
=begin
clear whole cache store
Cache.clear
=end
def self.clear
# workaround, set test cache before clear whole cache, Rails.cache.clear complains about not existing cache dir
Cache.write('test', 1)
Rails.cache.clear
end
end