Class: OnlineGHAProvider::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/gh-archive.rb

Instance Method Summary collapse

Constructor Details

#initialize(folder = Dir.mktmpdir, max_size = 100) ⇒ Cache

Returns a new instance of Cache.



170
171
172
173
174
175
# File 'lib/gh-archive.rb', line 170

def initialize(folder = Dir.mktmpdir, max_size = 100)
    @cache = {}
    @max_size = max_size
    @folder = folder
    @mutex = Mutex.new
end

Instance Method Details

#full?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/gh-archive.rb', line 215

def full?
    self.size >= @max_size
end

#get(name) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/gh-archive.rb', line 187

def get(name)
    @mutex.synchronize do
        return File.read(@cache[name])
    end
ensure
    self.unload(name)
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/gh-archive.rb', line 211

def has?(name)
    return @cache.has_key?(name)
end

#put(name, content) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/gh-archive.rb', line 177

def put(name, content)
    File.open("#@folder/#{name}", 'w') do |f|
        f << content
    end
    
    @mutex.synchronize do
        @cache[name] = value
    end
end

#sizeObject



205
206
207
208
209
# File 'lib/gh-archive.rb', line 205

def size
    @mutex.synchronize do
        @cache.size
    end
end

#unload(name) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/gh-archive.rb', line 195

def unload(name)
    File.unlink(@cache[name])
    
    @mutex.synchronize do
        @cache.delete(name)
    end
    
    return true
end