Module: FastCache::Memcache::Protocol

Included in:
Connection
Defined in:
lib/fastcache/memcache/protocol.rb

Instance Method Summary collapse

Instance Method Details

#add(key, value, expiry = 0) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fastcache/memcache/protocol.rb', line 34

def add(key, value, expiry = 0)
  data = value.to_s
  router.route(key) {|node|
    node.write "add #{key} 0 #{expiry} #{data.size}\r\n#{data}\r\n"
    resp = node.gets
    if resp == "STORED\r\n"
      FastCache::Maybe.just(data)
    else
      FastCache::Maybe.nothing
    end
  }
rescue FastCache::Memcache::ProtocolError
  FastCache::Maybe.nothing
end

#casObject



74
75
76
# File 'lib/fastcache/memcache/protocol.rb', line 74

def cas(*)
  fail 'Not implemented'
end

#decr(key, count) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fastcache/memcache/protocol.rb', line 90

def decr(key, count)
  router.route(key) {|node|
    node.write "decr #{key} #{count}\r\n"
    resp = node.gets
    resp == "NOT_FOUND\r\n" ?
      nil :
      resp.to_i
  }
rescue FastCache::Memcache::ProtocolError
  FastCache::Maybe.nothing
end

#delete(key, expiry = 0) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/fastcache/memcache/protocol.rb', line 64

def delete(key, expiry = 0)
  router.route(key) {|node|
    node.write "delete #{key} #{expiry}\r\n"
    resp = node.gets
    resp == "DELETED\r\n"
  }
rescue FastCache::Memcache::ProtocolError
  FastCache::Maybe.nothing
end

#flush_all_caches(delay = nil) ⇒ Object



102
103
104
105
106
107
# File 'lib/fastcache/memcache/protocol.rb', line 102

def flush_all_caches(delay = nil)
  router.each {|node|
    node.write "flush_all #{delay} noreply\r\n"
  }
  self
end

#flush_cache(key, delay = nil) ⇒ Object



109
110
111
112
113
114
# File 'lib/fastcache/memcache/protocol.rb', line 109

def flush_cache(key, delay = nil)
  router.route(key) {|node|
    node.write "flush_all #{delay} noreply\r\n"
  }
  self
end

#get(key) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fastcache/memcache/protocol.rb', line 3

def get(key)
  router.route(key) {|node|
    node.write "get #{key}\r\n"
    resp = node.gets
    break FastCache::Maybe.nothing if resp == "END\r\n"
    resp =~ /(\d+)\r/
    value = node.read $1.to_i
    node.read 2 # \r\n
    node.gets # END\r\n
    break FastCache::Maybe.just(value) unless value.nil?
    FastCache::Maybe.nothing
  }
rescue FastCache::Memcache::ProtocolError
  FastCache::Maybe.nothing
end

#incr(key, count) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fastcache/memcache/protocol.rb', line 78

def incr(key, count)
  router.route(key) {|node|
    node.write "incr #{key} #{count}\r\n"
    resp = node.gets
    resp == "NOT_FOUND\r\n" ?
      nil :
      resp.to_i
  }
rescue FastCache::Memcache::ProtocolError
  FastCache::Maybe.nothing
end

#replace(key, value, expiry = 0) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastcache/memcache/protocol.rb', line 49

def replace(key, value, expiry = 0)
  data = value.to_s
  router.route(key) {|node|
    node.write "replace #{key} 0 #{expiry} #{data.size}\r\n#{data}\r\n"
    resp = node.gets
    if resp == "STORED\r\n"
      FastCache::Maybe.just(data)
    else
      FastCache::Maybe.nothing
    end
  }
rescue FastCache::Memcache::ProtocolError
  FastCache::Maybe.nothing
end

#reset_connectionsObject



120
121
122
# File 'lib/fastcache/memcache/protocol.rb', line 120

def reset_connections
  router.each {|node| node.close}
end

#set(key, value, expiry = 0) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fastcache/memcache/protocol.rb', line 19

def set(key, value, expiry = 0)
  value = value.to_s
  router.route(key) {|node|
    node.write "set #{key} 0 #{expiry} #{value.size}\r\n#{value}\r\n"
    result = node.gets
    if result == "STORED\r\n"
      FastCache::Maybe.just(value)
    else
      FastCache::Maybe.nothing
    end
  }
rescue FastCache::Memcache::ProtocolError
  FastCache::Maybe.nothing
end

#statsObject



116
117
118
# File 'lib/fastcache/memcache/protocol.rb', line 116

def stats(*)
  fail 'Not implemented'
end