Class: TinyMemcache
- Inherits:
-
Object
- Object
- TinyMemcache
- Defined in:
- lib/tiny_memcache.rb
Overview
a small in-memory cache, barely more than a Hash.
it supports the following commands from the memcache protocol:
* get <key>
* set <key> <value>
* del <key>
the key and value lengths are configurable, but default to 1k and 64k respectively.
example:
cache = TinyMemcache.new
cache.set("foo", "bar")
cache.get("foo") #=> "bar"
cache.del("foo")
cache.get("foo") #=> nil
Defined Under Namespace
Classes: KeyTooLongError, ValueTooLongError
Instance Attribute Summary collapse
-
#total_key_size ⇒ Object
readonly
Returns the value of attribute total_key_size.
-
#total_keys ⇒ Object
readonly
Returns the value of attribute total_keys.
-
#total_value_size ⇒ Object
readonly
Returns the value of attribute total_value_size.
Instance Method Summary collapse
- #del(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(max_key_size: 1_024, max_value_size: 65_536) ⇒ TinyMemcache
constructor
initializes a new tiny_memcache object.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(max_key_size: 1_024, max_value_size: 65_536) ⇒ TinyMemcache
initializes a new tiny_memcache object.
options:
:max_key_length - the maximum length of a key in bytes (default: 1024)
:max_value_length - the maximum length of a value in bytes (default: 65536)
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/tiny_memcache.rb', line 33 def initialize(max_key_size: 1_024, max_value_size: 65_536) @data = {} @total_keys = 0 @total_key_size = 0 @total_value_size = 0 @max_key_length = max_key_size @max_value_length = max_value_size end |
Instance Attribute Details
#total_key_size ⇒ Object (readonly)
Returns the value of attribute total_key_size.
22 23 24 |
# File 'lib/tiny_memcache.rb', line 22 def total_key_size @total_key_size end |
#total_keys ⇒ Object (readonly)
Returns the value of attribute total_keys.
22 23 24 |
# File 'lib/tiny_memcache.rb', line 22 def total_keys @total_keys end |
#total_value_size ⇒ Object (readonly)
Returns the value of attribute total_value_size.
22 23 24 |
# File 'lib/tiny_memcache.rb', line 22 def total_value_size @total_value_size end |
Instance Method Details
#del(key) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/tiny_memcache.rb', line 58 def del(key) @total_keys -= 1 @total_key_size -= ObjectSpace.memsize_of(key) @total_value_size -= ObjectSpace.memsize_of(get(key)) @data.delete(key) end |
#get(key) ⇒ Object
44 45 46 |
# File 'lib/tiny_memcache.rb', line 44 def get(key) @data[key] end |
#set(key, value) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/tiny_memcache.rb', line 48 def set(key, value) raise KeyTooLongError if ObjectSpace.memsize_of(key) > @max_key_length raise ValueTooLongError if ObjectSpace.memsize_of(value) > @max_value_length @total_keys += 1 @total_key_size += ObjectSpace.memsize_of(key) @total_value_size += ObjectSpace.memsize_of(value) @data[key] = value end |