Class: Rack::MiniProfiler::FileStore
Defined Under Namespace
Classes: CacheCleanupThread, FileCache
Constant Summary
collapse
- EXPIRES_IN_SECONDS =
60 * 60 * 24
Instance Method Summary
collapse
#diagnostics
Constructor Details
#initialize(args = nil) ⇒ FileStore
Returns a new instance of FileStore.
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
68
69
70
71
72
73
74
75
76
|
# File 'lib/mini_profiler/storage/file_store.rb', line 36
def initialize(args = nil)
args ||= {}
@path = args[:path]
@expires_in_seconds = args[:expires_in] || EXPIRES_IN_SECONDS
raise ArgumentError.new :path unless @path
@timer_struct_cache = FileCache.new(@path, "mp_timers")
@timer_struct_lock = Mutex.new
@user_view_cache = FileCache.new(@path, "mp_views")
@user_view_lock = Mutex.new
me = self
t = CacheCleanupThread.new do
interval = 10
cleanup_cache_cycle = 3600
cycle_count = 1
begin
until Thread.current[:should_exit] do
if cycle_count * interval >= cleanup_cache_cycle
cycle_count = 1
me.cleanup_cache
end
sleep(interval)
cycle_count += 1
end
rescue
end
end
at_exit { t[:should_exit] = true }
t
end
|
Instance Method Details
#cleanup_cache ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/mini_profiler/storage/file_store.rb', line 115
def cleanup_cache
files = Dir.entries(@path)
@timer_struct_lock.synchronize {
files.each do |f|
f = @path + '/' + f
::File.delete f if ::File.basename(f) =~ /^mp_timers/ and (Time.now - ::File.mtime(f)) > @expires_in_seconds
end
}
@user_view_lock.synchronize {
files.each do |f|
f = @path + '/' + f
::File.delete f if ::File.basename(f) =~ /^mp_views/ and (Time.now - ::File.mtime(f)) > @expires_in_seconds
end
}
end
|
#get_unviewed_ids(user) ⇒ Object
109
110
111
112
113
|
# File 'lib/mini_profiler/storage/file_store.rb', line 109
def get_unviewed_ids(user)
@user_view_lock.synchronize {
@user_view_cache[user]
}
end
|
#load(id) ⇒ Object
84
85
86
87
88
|
# File 'lib/mini_profiler/storage/file_store.rb', line 84
def load(id)
@timer_struct_lock.synchronize {
@timer_struct_cache[id]
}
end
|
#save(page_struct) ⇒ Object
78
79
80
81
82
|
# File 'lib/mini_profiler/storage/file_store.rb', line 78
def save(page_struct)
@timer_struct_lock.synchronize {
@timer_struct_cache[page_struct[:id]] = page_struct
}
end
|
#set_unviewed(user, id) ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/mini_profiler/storage/file_store.rb', line 90
def set_unviewed(user, id)
@user_view_lock.synchronize {
current = @user_view_cache[user]
current = [] unless Array === current
current << id
@user_view_cache[user] = current.uniq
}
end
|
#set_viewed(user, id) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/mini_profiler/storage/file_store.rb', line 99
def set_viewed(user, id)
@user_view_lock.synchronize {
@user_view_cache[user] ||= []
current = @user_view_cache[user]
current = [] unless Array === current
current.delete(id)
@user_view_cache[user] = current.uniq
}
end
|