Module: LightRedisCache

Defined in:
lib/light_redis_cache.rb,
lib/light_redis_cache/version.rb,
lib/light_redis_cache/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.socketObject

Returns the value of attribute socket.



9
10
11
# File 'lib/light_redis_cache.rb', line 9

def socket
  @socket
end

Class Method Details

.clearvoid

This method returns an undefined value.

Clear database

See Also:



129
130
131
132
133
# File 'lib/light_redis_cache.rb', line 129

def clear
  open_socket
  @socket.puts("FLUSHALL")
  close_socket
end

.configurationObject



15
16
17
# File 'lib/light_redis_cache.rb', line 15

def configuration
  @configuration ||= LightRedisCache::Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



11
12
13
# File 'lib/light_redis_cache.rb', line 11

def configure
  yield(configuration)
end

.delete(keys) ⇒ void

This method returns an undefined value.

Delete keys

Parameters:

  • keys (Array)

See Also:



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/light_redis_cache.rb', line 113

def delete keys
  open_socket
  request = ""
  request_length = 0
  keys.each do |key|
    request.insert(-1, "$#{ key.length }\r\n#{ key }\r\n")
    request_length +=1
  end
  @socket.write("*#{ request_length + 1 }\r\n$3\r\nDEL\r\n#{ request }")
  close_socket
end

.delete_matched(matcher) ⇒ void?

Remove keys corresponding to matcher

Parameters:

  • matcher (String)

Returns:

  • (void, nil)


69
70
71
72
73
# File 'lib/light_redis_cache.rb', line 69

def delete_matched matcher
  matched_keys = get_matched_keys matcher

  delete matched_keys if matched_keys
end

.fetch(key, expires_in: 86400, &block) ⇒ void

This method returns an undefined value.

Get the value of a key. If the key does not exist, call a block, set the result and return it

Parameters:

  • key (String)
  • expires_in (Integer) (defaults to: 86400)
    • default value : 86400 seconds (1 day)

  • block (Proc)


54
55
56
57
58
59
60
61
62
63
# File 'lib/light_redis_cache.rb', line 54

def fetch key, expires_in: 86400, &block
  result = get(key)
  if result == nil
    value = block.call
    set(key, value, expires_in: expires_in)
    value
  else
    result
  end
end

.get(key) ⇒ String, ...

Get the value of a key

Parameters:

  • key (String)

Returns:

  • (String, Integer, Array, Hash)

    value

See Also:



24
25
26
27
28
29
30
# File 'lib/light_redis_cache.rb', line 24

def get key
  open_socket
  @socket.write("*2\r\n$3\r\nGET\r\n$#{ key.length }\r\n#{ key }\r\n")
  value = @socket.gets == "$-1\r\n" ? nil : JSON.parse((@socket.gets).gsub("\r\n", "").force_encoding("iso-8859-1").encode!("utf-8"))
  close_socket
  value
end

.get_matched_keys(matcher) ⇒ Array<String>

Get keys corresponding to matcher

Supported glob-style patterns:

h?llo matches hello, hallo and hxllo h*llo matches hllo and heeeello hllo matches hello and hallo, but not hillo hllo matches hallo, hbllo, … but not hello hllo matches hallo and hbllo

Parameters:

  • matcher (String)

Returns:

  • (Array<String>)

See Also:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/light_redis_cache.rb', line 88

def get_matched_keys matcher
  open_socket
  @socket.puts("KEYS #{ matcher }")
  first_result = @socket.gets

  if first_result.include?("*")
    matched_keys_number = first_result.gsub("*", "").gsub("\r\n", "").to_i
    keys = []
    (1..(matched_keys_number*2)).collect do |index|
      if index.even?
        keys.push((@socket.gets).gsub("\r\n", ""))
      else
        @socket.gets
      end
    end
  end
  close_socket
  keys
end

.set(key, value, expires_in: 86400) ⇒ nil

Set key to hold the value

Parameters:

  • key (String)
  • value (String, Integer, Array, Hash)
  • expires_in (Integer) (defaults to: 86400)
    • default value : 86400 seconds (1 day)

Returns:

  • (nil)

See Also:



39
40
41
42
43
44
45
# File 'lib/light_redis_cache.rb', line 39

def set key, value, expires_in: 86400
  open_socket
  value = value.to_json.encode("iso-8859-1").force_encoding("utf-8")
  @socket.write("*3\r\n$3\r\nSET\r\n$#{ key.length }\r\n#{ key }\r\n$#{ value.length }\r\n#{ value }\r\n")
  @socket.write("*3\r\n$6\r\nEXPIRE\r\n$#{ key.length }\r\n#{ key }\r\n$#{ expires_in.to_s.length }\r\n#{ expires_in }\r\n")
  close_socket
end