Class: Rack::MiniProfiler::FileStore
Defined Under Namespace
Classes: CacheCleanupThread, FileCache
Constant Summary
collapse
- EXPIRES_IN_SECONDS =
60 * 60 * 24
AbstractStore::MAX_TOKEN_AGE
Instance Method Summary
collapse
#diagnostics, #fetch_snapshots_group, #fetch_snapshots_overview, #load_snapshot, #push_snapshot, #should_take_snapshot?, #snapshots_group, #snapshots_overview
Constructor Details
#initialize(args = nil) ⇒ FileStore
Returns a new instance of FileStore.
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/mini_profiler/storage/file_store.rb', line 51
def initialize(args = nil)
args ||= {}
@path = args[:path]
@expires_in_seconds = args[:expires_in] || EXPIRES_IN_SECONDS
raise ArgumentError.new :path unless @path
FileUtils.mkdir_p(@path) unless ::File.exist?(@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
@auth_token_cache = FileCache.new(@path, "tokens")
@auth_token_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 }
end
|
Instance Method Details
#allowed_tokens ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/mini_profiler/storage/file_store.rb', line 145
def allowed_tokens
@auth_token_lock.synchronize {
token1, token2, cycle_at = @auth_token_cache[""]
unless cycle_at && (Float === cycle_at) && (cycle_at > Process.clock_gettime(Process::CLOCK_MONOTONIC))
token2 = token1
token1 = SecureRandom.hex
cycle_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + Rack::MiniProfiler::AbstractStore::MAX_TOKEN_AGE
end
@auth_token_cache[""] = [token1, token2, cycle_at]
[token1, token2].compact
}
end
|
#cleanup_cache ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/mini_profiler/storage/file_store.rb', line 161
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/) && ((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/) && ((Time.now - ::File.mtime(f)) > @expires_in_seconds)
end
}
end
|
#flush_tokens ⇒ Object
139
140
141
142
143
|
# File 'lib/mini_profiler/storage/file_store.rb', line 139
def flush_tokens
@auth_token_lock.synchronize {
@auth_token_cache[""] = nil
}
end
|
#get_unviewed_ids(user) ⇒ Object
133
134
135
136
137
|
# File 'lib/mini_profiler/storage/file_store.rb', line 133
def get_unviewed_ids(user)
@user_view_lock.synchronize {
@user_view_cache[user]
}
end
|
#load(id) ⇒ Object
102
103
104
105
106
|
# File 'lib/mini_profiler/storage/file_store.rb', line 102
def load(id)
@timer_struct_lock.synchronize {
@timer_struct_cache[id]
}
end
|
#save(page_struct) ⇒ Object
96
97
98
99
100
|
# File 'lib/mini_profiler/storage/file_store.rb', line 96
def save(page_struct)
@timer_struct_lock.synchronize {
@timer_struct_cache[page_struct[:id]] = page_struct
}
end
|
#set_all_unviewed(user, ids) ⇒ Object
127
128
129
130
131
|
# File 'lib/mini_profiler/storage/file_store.rb', line 127
def set_all_unviewed(user, ids)
@user_view_lock.synchronize {
@user_view_cache[user] = ids.uniq
}
end
|
#set_unviewed(user, id) ⇒ Object
108
109
110
111
112
113
114
115
|
# File 'lib/mini_profiler/storage/file_store.rb', line 108
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
117
118
119
120
121
122
123
124
125
|
# File 'lib/mini_profiler/storage/file_store.rb', line 117
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
|