tiny_memcache 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/tiny_memcache.rb +68 -0
- metadata +40 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: cc5d7597a0b2d914021e48cb32e795e356f368591123806d102b09b02f393549
|
|
4
|
+
data.tar.gz: ba07daba44061fec4ca3ce9be73eb3c1fe42f2b78eae2b2cefa0abc1b21f98c4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b9a4f437fa04ee9440bf40473a40411361438bc781579624b05175658d0c6dec4fc3aeb5c870e1dff53abb40d7c9518535ca6963457a2882f1276267a1db4d56
|
|
7
|
+
data.tar.gz: e47757a016a334341c9f889953e7d588d6f50d8be65b6084b0dd8838d83d6b4bfe0d861a0a222f33cad8050f173b94409c0d44dac3f89389e9a276c56248792d
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require 'objspace'
|
|
2
|
+
|
|
3
|
+
# a small in-memory cache, barely more than a Hash.
|
|
4
|
+
#
|
|
5
|
+
# it supports the following commands from the memcache protocol:
|
|
6
|
+
#
|
|
7
|
+
# * get <key>
|
|
8
|
+
# * set <key> <value>
|
|
9
|
+
# * del <key>
|
|
10
|
+
#
|
|
11
|
+
# the key and value lengths are configurable, but default to 1k and 64k
|
|
12
|
+
# respectively.
|
|
13
|
+
#
|
|
14
|
+
# example:
|
|
15
|
+
#
|
|
16
|
+
# cache = TinyMemcache.new
|
|
17
|
+
# cache.set("foo", "bar")
|
|
18
|
+
# cache.get("foo") #=> "bar"
|
|
19
|
+
# cache.del("foo")
|
|
20
|
+
# cache.get("foo") #=> nil
|
|
21
|
+
class TinyMemcache
|
|
22
|
+
attr_reader :total_keys,
|
|
23
|
+
:total_key_size,
|
|
24
|
+
:total_value_size
|
|
25
|
+
|
|
26
|
+
# initializes a new tiny_memcache object.
|
|
27
|
+
#
|
|
28
|
+
# options:
|
|
29
|
+
#
|
|
30
|
+
# :max_key_length - the maximum length of a key in bytes (default: 1024)
|
|
31
|
+
# :max_value_length - the maximum length of a value in bytes (default: 65536)
|
|
32
|
+
#
|
|
33
|
+
def initialize(max_key_size: 1_024, max_value_size: 65_536)
|
|
34
|
+
@data = {}
|
|
35
|
+
|
|
36
|
+
@total_keys = 0
|
|
37
|
+
@total_key_size = 0
|
|
38
|
+
@total_value_size = 0
|
|
39
|
+
|
|
40
|
+
@max_key_length = max_key_size
|
|
41
|
+
@max_value_length = max_value_size
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def get(key)
|
|
45
|
+
@data[key]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def set(key, value)
|
|
49
|
+
raise KeyTooLongError if ObjectSpace.memsize_of(key) > @max_key_length
|
|
50
|
+
raise ValueTooLongError if ObjectSpace.memsize_of(value) > @max_value_length
|
|
51
|
+
|
|
52
|
+
@total_keys += 1
|
|
53
|
+
@total_key_size += ObjectSpace.memsize_of(key)
|
|
54
|
+
@total_value_size += ObjectSpace.memsize_of(value)
|
|
55
|
+
@data[key] = value
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def del(key)
|
|
59
|
+
@total_keys -= 1
|
|
60
|
+
@total_key_size -= ObjectSpace.memsize_of(key)
|
|
61
|
+
@total_value_size -= ObjectSpace.memsize_of(get(key))
|
|
62
|
+
|
|
63
|
+
@data.delete(key)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
class KeyTooLongError < StandardError; end
|
|
67
|
+
class ValueTooLongError < StandardError; end
|
|
68
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tiny_memcache
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jeff Lunt
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2025-12-18 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: a tiny in-memory cache
|
|
13
|
+
email: jefflunt@gmail.com
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- lib/tiny_memcache.rb
|
|
19
|
+
homepage: https://github.com/jefflunt/tiny_memcache
|
|
20
|
+
licenses:
|
|
21
|
+
- MIT
|
|
22
|
+
metadata: {}
|
|
23
|
+
rdoc_options: []
|
|
24
|
+
require_paths:
|
|
25
|
+
- lib
|
|
26
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
27
|
+
requirements:
|
|
28
|
+
- - ">="
|
|
29
|
+
- !ruby/object:Gem::Version
|
|
30
|
+
version: '0'
|
|
31
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: '0'
|
|
36
|
+
requirements: []
|
|
37
|
+
rubygems_version: 3.6.3
|
|
38
|
+
specification_version: 4
|
|
39
|
+
summary: a tiny in-memory cache
|
|
40
|
+
test_files: []
|