Class: Tina4::CacheBackends::MemcachedBackend
- Inherits:
-
BaseBackend
- Object
- BaseBackend
- Tina4::CacheBackends::MemcachedBackend
- Defined in:
- lib/tina4/cache_backends/memcached_backend.rb
Overview
Memcached backend using the zero-dependency text protocol over TCP (parity with Python _MemcachedBackend). Keys are SHA-256 hashed to stay within memcached’s 250-char / no-space key constraints. Memcached has no auth, so credentials are ignored.
Constant Summary collapse
- PREFIX =
"tina4:cache:"
Instance Method Summary collapse
- #available? ⇒ Boolean
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(url: "memcached://localhost:11211", max_entries: 1000) ⇒ MemcachedBackend
constructor
A new instance of MemcachedBackend.
- #name ⇒ Object
- #set(key, value, ttl) ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(url: "memcached://localhost:11211", max_entries: 1000) ⇒ MemcachedBackend
Returns a new instance of MemcachedBackend.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 17 def initialize(url: "memcached://localhost:11211", max_entries: 1000) cleaned = url.sub(%r{^memcached://}, "").sub(%r{^memcache://}, "") parts = cleaned.split("/").first.to_s.split(":") @host = parts[0].nil? || parts[0].empty? ? "localhost" : parts[0] @port = parts[1] && !parts[1].empty? ? parts[1].to_i : 11_211 @max_entries = max_entries @hits = 0 @misses = 0 @available = command("version\r\n", "\r\n").start_with?("VERSION") end |
Instance Method Details
#available? ⇒ Boolean
28 29 30 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 28 def available? @available end |
#clear ⇒ Object
58 59 60 61 62 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 58 def clear @hits = 0 @misses = 0 command("flush_all\r\n", "\r\n") end |
#delete(key) ⇒ Object
54 55 56 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 54 def delete(key) command("delete #{mc_key(key)}\r\n", "\r\n").start_with?("DELETED") end |
#get(key) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 32 def get(key) resp = command("get #{mc_key(key)}\r\n", "END\r\n") if resp.start_with?("VALUE") begin header, rest = resp.split("\r\n", 2) nbytes = header.split[3].to_i @hits += 1 return JSON.parse(rest[0, nbytes]) rescue StandardError end end @misses += 1 nil end |
#name ⇒ Object
75 76 77 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 75 def name "memcached" end |
#set(key, value, ttl) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 47 def set(key, value, ttl) data = JSON.generate(value) exptime = ttl > 0 ? ttl : 0 payload = "set #{mc_key(key)} 0 #{exptime} #{data.bytesize}\r\n#{data}\r\n" command(payload, "\r\n") end |
#stats ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tina4/cache_backends/memcached_backend.rb', line 64 def stats size = 0 resp = command("stats\r\n", "END\r\n") resp.split("\r\n").each do |line| if line.start_with?("STAT curr_items ") size = line.split[2].to_i end end { hits: @hits, misses: @misses, size: size, backend: "memcached" } end |