Class: Nanite::CertificateCache
- Defined in:
- lib/nanite/security/certificate_cache.rb
Overview
Implements a simple LRU cache: items that are the least accessed are deleted first.
Constant Summary collapse
- DEFAULT_CACHE_MAX_COUNT =
Max number of items to keep in memory
100
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Delete item from cache.
-
#get(key) ⇒ Object
(also: #[])
Retrieve item from cache Store item returned by given block if any.
-
#initialize(max_count = DEFAULT_CACHE_MAX_COUNT) ⇒ CertificateCache
constructor
Initialize cache.
-
#put(key, item) ⇒ Object
(also: #[]=)
Add item to cache.
Constructor Details
#initialize(max_count = DEFAULT_CACHE_MAX_COUNT) ⇒ CertificateCache
Initialize cache
11 12 13 14 15 |
# File 'lib/nanite/security/certificate_cache.rb', line 11 def initialize(max_count = DEFAULT_CACHE_MAX_COUNT) @items = {} @list = [] @max_count = max_count end |
Instance Method Details
#delete(key) ⇒ Object
Delete item from cache
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/nanite/security/certificate_cache.rb', line 51 def delete(key) c = @items[key] if c @items.delete(key) @list.each_index do |i| if @list[i] == key @list.delete_at(i) break end end c end end |
#get(key) ⇒ Object Also known as: []
Retrieve item from cache Store item returned by given block if any
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nanite/security/certificate_cache.rb', line 33 def get(key) if @items.include?(key) @list.each_index do |i| if @list[i] == key @list.delete_at(i) break end end @list.push(key) @items[key] else return nil unless block_given? self[key] = yield end end |
#put(key, item) ⇒ Object Also known as: []=
Add item to cache
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/nanite/security/certificate_cache.rb', line 18 def put(key, item) if @items.include?(key) delete(key) end if @list.size == @max_count delete(@list.first) end @items[key] = item @list.push(key) item end |