blob: bdd830a13565dc1542dee215bb211cd7cfd3317b (
plain)
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
|
require 'thread'
require 'singleton'
##
# A generic, thread-safe in-memory cache. It's used for caching
# RDoc::TemplatePage objects when generating RDoc output.
class RDoc::Cache
include Singleton
##
# Creates a new, empty cache
def initialize
@contents = {}
@lock = Mutex.new
end
##
# Checks whether there's a value in the cache with key +key+. If so, then
# that value will be returned. Otherwise, the given block will be run, and
# its return value will be put into the cache, and returned.
def cache(key)
@lock.synchronize do
@contents[key] ||= yield
end
end
##
# Clears the contents of the cache
def clear
@lock.synchronize do
@contents.clear
end
end
end
|