Class: Shellac::Storage_engine::Base
- Inherits:
-
Object
- Object
- Shellac::Storage_engine::Base
- Defined in:
- lib/shellac/storage_engine.rb
Direct Known Subclasses
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #_default_args ⇒ Object
- #delete(k) ⇒ Object
- #get(k) ⇒ Object
-
#initialize(args = {}) ⇒ Base
constructor
A new instance of Base.
- #keys ⇒ Object
- #length ⇒ Object (also: #size)
- #new_cache_control_thread ⇒ Object
- #set(k, v) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Base
Returns a new instance of Base.
4 5 6 7 8 9 |
# File 'lib/shellac/storage_engine.rb', line 4 def initialize( args = {} ) @config = _default_args.merge( args ) @cache_control_thread = new_cache_control_thread @cache_size = 0 @cache = {} end |
Instance Method Details
#[](k) ⇒ Object
33 34 35 |
# File 'lib/shellac/storage_engine.rb', line 33 def []( k ) @cache[ k ] end |
#[]=(k, v) ⇒ Object
41 42 43 44 |
# File 'lib/shellac/storage_engine.rb', line 41 def []=( k, v ) @cache_size += v.to_s.length @cache[ k ] = v end |
#_default_args ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/shellac/storage_engine.rb', line 11 def _default_args { preload: {}, length_limit: 1000, size_limit: 1024 * 1024 * 20, trim_interval: 30 } end |
#delete(k) ⇒ Object
54 55 56 |
# File 'lib/shellac/storage_engine.rb', line 54 def delete( k ) @cache.delete( k ) end |
#get(k) ⇒ Object
37 38 39 |
# File 'lib/shellac/storage_engine.rb', line 37 def get( k ) self[ k ] end |
#keys ⇒ Object
50 51 52 |
# File 'lib/shellac/storage_engine.rb', line 50 def keys @cache.keys end |
#length ⇒ Object Also known as: size
58 59 60 |
# File 'lib/shellac/storage_engine.rb', line 58 def length @cache.length end |
#new_cache_control_thread ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/shellac/storage_engine.rb', line 20 def new_cache_control_thread Thread.new do sleep( @config[ :trim_interval ] ) while @cache_size > @config[ :size_limit ] # Trim Cache -- stupid algorithm just randomly deletes things # until it is small enough sz = @cache.delete( @cache.keys[ rand( @cache.length ) ] ).to_s.length @cache_size -= sz end end end |
#set(k, v) ⇒ Object
46 47 48 |
# File 'lib/shellac/storage_engine.rb', line 46 def set( k, v ) self[ k ] = v end |