Class: NOMS::Command::UserAgent::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/noms/command/useragent/cache.rb

Constant Summary collapse

@@format_version =
'0'
@@max_cache_size =
10 * 1024 * 1024
@@trim_cache_size =
8 * 1024 * 1024
@@location =
File.join(NOMS::Command.home, 'cache', @@format_version)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/noms/command/useragent/cache.rb', line 44

def initialize
    ensure_dir @@location
    @meta = PStore.new File.join(@@location, 'metadata.db')
    @meta.transaction do
        @meta[:cache_size] ||= 0
        @meta[:file_count] ||= 0

        if @meta[:cache_size] > @@max_cache_size
            trim!
        end
    end
end

Class Method Details

.clear!Object



30
31
32
33
34
35
36
37
38
# File 'lib/noms/command/useragent/cache.rb', line 30

def self.clear!
    FileUtils.rm_r @@location if File.directory? @@location
    ensure_dir @@location
    meta = PStore.new(File.join(@@location, 'metadata.db'))
    meta.transaction do
        meta[:cache_size] = 0
        meta[:file_count] = 0
    end
end

Instance Method Details

#cache_dir(key = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/noms/command/useragent/cache.rb', line 66

def cache_dir(key=nil)
    if key
        File.join(@@location, 'data', key[0 .. 1])
    else
        File.join(@@location, 'data')
    end
end

#clear!Object



57
58
59
60
61
62
63
64
# File 'lib/noms/command/useragent/cache.rb', line 57

def clear!
    FileUtils.rm_r @@location if File.directory? @@location
    ensure_dir @@location
    @meta.transaction do
        @meta[:cache_size] = 0
        @meta[:file_count] = 0
    end
end

#ensure_dir(dir) ⇒ Object



40
41
42
# File 'lib/noms/command/useragent/cache.rb', line 40

def ensure_dir(dir)
    FileUtils.mkdir_p dir unless File.directory? dir
end

#freshen(key) ⇒ Object



87
88
89
90
# File 'lib/noms/command/useragent/cache.rb', line 87

def freshen(key)
    cache_file = File.join(cache_dir(key), key)
    File.utime(Time.now, Time.now, cache_file)
end

#get(key) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/noms/command/useragent/cache.rb', line 92

def get(key)
    cache_file = File.join(cache_dir(key), key)
    obj = nil
    if File.exist? cache_file
        obj = File.open(cache_file, 'r') do |fh|
            fh.read
            # Marshal.load(fh)
        end
    end
    obj
end

#set(key, item_data) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/noms/command/useragent/cache.rb', line 74

def set(key, item_data)
    size = 0
    ensure_dir cache_dir(key)
    File.open(File.join(cache_dir(key), key), 'w') do |fh|
        fh.write item_data
        size = fh.stat.size
    end
    @meta.transaction do
        @meta[:cache_size] += size
        @meta[:file_count] += 1
    end
end

#trim!Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/noms/command/useragent/cache.rb', line 104

def trim!
    # Trim cache, all within PStore transaction
    cache_size = 0
    files = Dir["#{@@location}/*/*"].map do |file|
        stat = File.stat(file)
        size = stat.size
        mtime = stat.mtime
        total += size
        [file, size, mtime]
    end.sort { |a, b| a[2] <=> b[2] }
    file_count = files.length
    while cache_size > @@trim_cache_size
        file, size, = files.shift
        File.unlink(file)
        cache_size -= size
    end
    @meta[:cache_size] = cache_size
    @meta[:file_count] = file_count
end