light_redis_cache 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83385a0dd07984d31d6c3778313cdd7d6d7ab7eb
4
- data.tar.gz: 37996d8e8f61f9b2b98f9fac3c8f9e608aae7ee0
3
+ metadata.gz: 17d34de15b0df4ade8a71c6147996537cf23683e
4
+ data.tar.gz: 86bd95b55c56940dcd01b71609b42d9c7b5a0d57
5
5
  SHA512:
6
- metadata.gz: 6d6bc4946d77e530ac1875de9069964bd3f1c3478b737a5ef547f76aab34fca64f80d3120c13a0c718c9c38991ff407bbd5f04765657c4b4333388c56b253ab0
7
- data.tar.gz: d8031143f139039be39422d844c672563637b322c2b3bc6fbf582fe81cd21cb8a9cdcc6f8d4925a3e87e1b23a4c2ec85c2ea4d635e44fd0887ff322b82ae3d4c
6
+ metadata.gz: 1d4bf4504656880897e3efe1cf4acbd0f2901a0ac8a721aabf2cb409cdeb32f7f29d0f939bc628bbe758434b3936ab3e466f74525ef56d14dae3eb640e0f8282
7
+ data.tar.gz: abf41096e51c3693a1441452a6a682faca0b9392232efe7590e0078f7f792a2374d955bd6a9fbace8af59e8d9b4d6d9e36691448e3110fec6abc53d706022163
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  # LightRedisCache
2
2
 
3
3
 
4
- It is a very basic implementation of a Redis client.
4
+ This gem is a very basic implementation of a Ruby client for Redis.
5
5
 
6
- :warning: **Do not to use it in production !** :warning:
6
+ It is not supposed to be used in production, the aim of this gem is to understand of Redis work and how to implement a Redis client to cache data.
7
7
 
8
- The aim of this gem is to understand of Redis work and how to implement a redis client to cache data.
9
- It can replace `Rails.cache` to connect to Redis in a Ruby on Rails application.
8
+ It creates a TCP socket between your app and your Redis server. A few methods let you communicate with your Redis server using a protocol called RESP (REdis Serialization Protocol). See https://redis.io/topics/protocol
9
+
10
+ In a Rails app, just use `LightRedisCache` instead of `Rails.cache`.
10
11
 
11
12
  ## Installation
12
13
 
@@ -26,7 +27,16 @@ Or install it yourself as:
26
27
  $ gem install light_redis_cache
27
28
  ```
28
29
 
29
- ## Usage
30
+ ## Configuration
31
+ Add this block, with the configuration of your Redis server, in your code :
32
+
33
+ ```ruby
34
+ LightRedisCache.configure do |config|
35
+ config.hostname = 'MY_HOSTNAME' # default: 'localhost'
36
+ config.port = 'MY_PORT' # default: '6379'
37
+ end
38
+ ```
39
+ If you have a Rails app, it should go in `config/initializers/light_redis_cache.rb`
30
40
 
31
41
  ## Development
32
42
 
@@ -35,5 +45,3 @@ After checking out the repo, run `bin/setup` to install dependencies.
35
45
  Set the config of the local redis server you want to use for the tests in `spec/local_redis_server.rb`.
36
46
 
37
47
  Then, run `rake spec` to run the tests.
38
-
39
- In your Rails app, replace `Rails.cache` by `LightRedisCache::Client.new(hostname: [YOUR_HOSTNAME], port: [YOURPORT])`
@@ -1,33 +1,56 @@
1
1
  require 'light_redis_cache/version'
2
+ require 'light_redis_cache/configuration'
2
3
  require 'socket'
3
4
  require 'json'
4
- require 'date'
5
5
 
6
6
  module LightRedisCache
7
- class Client
7
+
8
+ class << self
8
9
  attr_accessor :socket
9
10
 
10
- def initialize hostname:, port:
11
- @hostname = hostname
12
- @port = port
11
+ def configure
12
+ yield(configuration)
13
+ end
14
+
15
+ def configuration
16
+ @configuration ||= LightRedisCache::Configuration.new
13
17
  end
14
18
 
19
+ # Get the value of a key
20
+ #
21
+ # @param [String] key
22
+ # @return [String, Integer, Array, Hash] value
23
+ # @see https://redis.io/commands/get GET command
15
24
  def get key
16
25
  open_socket
17
- @socket.puts("GET #{ key }")
18
- value = @socket.gets == "$-1\r\n" ? nil : JSON.parse(@socket.gets.gsub(/\$\d+/, "").gsub("\r\n", ""))
26
+ @socket.write("*2\r\n$3\r\nGET\r\n$#{ key.length }\r\n#{ key }\r\n")
27
+ value = @socket.gets == "$-1\r\n" ? nil : JSON.parse((@socket.gets).gsub("\r\n", "").force_encoding("iso-8859-1").encode!("utf-8"))
19
28
  close_socket
20
29
  value
21
30
  end
22
31
 
23
- def set key, value, expires_in:
32
+ # Set key to hold the value
33
+ #
34
+ # @param [String] key
35
+ # @param [String, Integer, Array, Hash] value
36
+ # @param [Integer] expires_in - default value : 86400 seconds (1 day)
37
+ # @return [nil]
38
+ # @see https://redis.io/commands/set SET command
39
+ def set key, value, expires_in: 86400
24
40
  open_socket
25
- value = value.to_json
41
+ value = value.to_json.encode("iso-8859-1").force_encoding("utf-8")
26
42
  @socket.write("*3\r\n$3\r\nSET\r\n$#{ key.length }\r\n#{ key }\r\n$#{ value.length }\r\n#{ value }\r\n")
27
43
  @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")
28
44
  close_socket
29
45
  end
30
46
 
47
+ # Get the value of a key. If the key does not exist,
48
+ # call a block, set the result and return it
49
+ #
50
+ # @param [String] key
51
+ # @param [Integer] expires_in - default value : 86400 seconds (1 day)
52
+ # @param [Proc] block
53
+ # @return [void]
31
54
  def fetch key, expires_in: 86400, &block
32
55
  result = get(key)
33
56
  if result == nil
@@ -39,8 +62,30 @@ module LightRedisCache
39
62
  end
40
63
  end
41
64
 
65
+ # Remove keys corresponding to matcher
66
+ #
67
+ # @param [String] matcher
68
+ # @return [void, nil]
42
69
  def delete_matched matcher
43
- #get matched keys
70
+ matched_keys = get_matched_keys matcher
71
+
72
+ delete matched_keys if matched_keys
73
+ end
74
+
75
+ # Get keys corresponding to matcher
76
+ #
77
+ # Supported glob-style patterns:
78
+ #
79
+ # h?llo matches hello, hallo and hxllo
80
+ # h*llo matches hllo and heeeello
81
+ # h[ae]llo matches hello and hallo, but not hillo
82
+ # h[^e]llo matches hallo, hbllo, ... but not hello
83
+ # h[a-b]llo matches hallo and hbllo
84
+ #
85
+ # @param [String] matcher
86
+ # @return [Array<String>]
87
+ # @see https://redis.io/commands/keys KEYS command
88
+ def get_matched_keys matcher
44
89
  open_socket
45
90
  @socket.puts("KEYS #{ matcher }")
46
91
  first_result = @socket.gets
@@ -55,19 +100,32 @@ module LightRedisCache
55
100
  @socket.gets
56
101
  end
57
102
  end
103
+ end
104
+ close_socket
105
+ keys
106
+ end
58
107
 
59
- #delete keys
60
- request = ""
61
- request_length = 0
62
- keys.each do |key|
63
- request.insert(-1, "$#{ key.length }\r\n#{ key }\r\n")
64
- request_length +=1
65
- end
66
- @socket.write("*#{ request_length + 1 }\r\n$3\r\nDEL\r\n#{ request }")
108
+ # Delete keys
109
+ #
110
+ # @param [Array] keys
111
+ # @return [void]
112
+ # @see https://redis.io/commands/del DEL command
113
+ def delete keys
114
+ open_socket
115
+ request = ""
116
+ request_length = 0
117
+ keys.each do |key|
118
+ request.insert(-1, "$#{ key.length }\r\n#{ key }\r\n")
119
+ request_length +=1
67
120
  end
121
+ @socket.write("*#{ request_length + 1 }\r\n$3\r\nDEL\r\n#{ request }")
68
122
  close_socket
69
123
  end
70
124
 
125
+ # Clear database
126
+ #
127
+ # @return [void]
128
+ # @see https://redis.io/commands/flushall FLUSHALL command
71
129
  def clear
72
130
  open_socket
73
131
  @socket.puts("FLUSHALL")
@@ -77,7 +135,7 @@ module LightRedisCache
77
135
  private
78
136
 
79
137
  def open_socket
80
- @socket = TCPSocket.new(@hostname, @port)
138
+ @socket = TCPSocket.new(@configuration.hostname, @configuration.port)
81
139
  end
82
140
 
83
141
  def close_socket
@@ -0,0 +1,11 @@
1
+ module LightRedisCache
2
+ class Configuration
3
+ attr_accessor :hostname
4
+ attr_accessor :port
5
+
6
+ def initialize
7
+ @hostname = nil
8
+ @port = nil
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module LightRedisCache
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Eric Leroy-Terquem"]
10
10
  spec.email = ["ericleroyterquem@gmail.com"]
11
11
 
12
- spec.summary = %q{A simple redis cache}
12
+ spec.summary = %q{A basic Ruby client for Redis}
13
13
  spec.homepage = "https://github.com/E-L-T/light_redis_cache"
14
14
  spec.license = "MIT"
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light_redis_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Leroy-Terquem
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-29 00:00:00.000000000 Z
11
+ date: 2021-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - bin/console
72
72
  - bin/setup
73
73
  - lib/light_redis_cache.rb
74
+ - lib/light_redis_cache/configuration.rb
74
75
  - lib/light_redis_cache/version.rb
75
76
  - light_redis_cache.gemspec
76
77
  homepage: https://github.com/E-L-T/light_redis_cache
@@ -96,5 +97,5 @@ rubyforge_project:
96
97
  rubygems_version: 2.6.13
97
98
  signing_key:
98
99
  specification_version: 4
99
- summary: A simple redis cache
100
+ summary: A basic Ruby client for Redis
100
101
  test_files: []