class Shellac::Storage_engine::Base

Public Class Methods

new( args = {} ) click to toggle source
# 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

Public Instance Methods

[]( k ) click to toggle source
# File lib/shellac/storage_engine.rb, line 33
def []( k )
  @cache[ k ]
end
[]=( k, v ) click to toggle source
# File lib/shellac/storage_engine.rb, line 41
def []=( k, v )
  @cache_size += v.to_s.length
  @cache[ k ] = v
end
_default_args() click to toggle source
# 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 ) click to toggle source
# File lib/shellac/storage_engine.rb, line 54
def delete( k )
  @cache.delete( k )
end
get( k ) click to toggle source
# File lib/shellac/storage_engine.rb, line 37
def get( k )
  self[ k ]
end
keys() click to toggle source
# File lib/shellac/storage_engine.rb, line 50
def keys
  @cache.keys
end
length() click to toggle source
# File lib/shellac/storage_engine.rb, line 58
def length
  @cache.length
end
Also aliased as: size
new_cache_control_thread() click to toggle source
# 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 ) click to toggle source
# File lib/shellac/storage_engine.rb, line 46
def set( k, v )
  self[ k ] = v
end
size()
Alias for: length