simple_redis 0.2.0 → 0.3.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 +4 -4
- data/README.md +27 -3
- data/lib/simple_redis/version.rb +2 -1
- data/lib/simple_redis.rb +28 -15
- data/test/dummy/app/models/department.rb +2 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20160412222842_create_departments.rb +9 -0
- data/test/dummy/db/schema.rb +22 -0
- data/test/dummy/log/development.log +58 -0
- data/test/dummy/test/fixtures/departments.yml +7 -0
- data/test/dummy/test/models/department_test.rb +7 -0
- metadata +14 -4
- data/test/dummy/dump.rdb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8f4c4a270c4100fa724e81a2388641ce7451bb6
|
4
|
+
data.tar.gz: d6bac06fd91939654889fef9c61f2285aff7e92f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1f072180a13d933b95aae2749b6f21d9c51f9988e1ea18d7cfc39cd4c7916d98f848ea77c2574b8cdf8975c26e61cdbdd7bcee65a3288aec4133f00175dbf7c
|
7
|
+
data.tar.gz: 4c13c46187c934d27393c9bd2666b9e36dcbbe7dc6fdee314015ec6fe903c000028fecaf8ff058c62164d0a30ba01f29409ae69c293f8585931e3068b23a2f78
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ Use Simple Redis just like you use Rails cache
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'simple_redis'
|
12
|
+
gem 'simple_redis', '~> 0.3.0'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
@@ -20,14 +20,17 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
$ gem install simple_redis
|
22
22
|
|
23
|
+
_Please use stable version 0.3.0_
|
24
|
+
|
23
25
|
## Usage
|
24
26
|
|
25
27
|
### fetch
|
26
28
|
You can use key and value parameter to cache the data
|
27
29
|
|
28
30
|
```ruby
|
29
|
-
SimpleRedis.fetch(key: 'department-list', value:
|
31
|
+
SimpleRedis.fetch(key: 'department-list', value: Department.all)
|
30
32
|
```
|
33
|
+
_Please note every cached object will be converted to json format using `to_json`_
|
31
34
|
|
32
35
|
OR You can use key and block to cache it
|
33
36
|
|
@@ -49,7 +52,7 @@ end
|
|
49
52
|
You can simply just set value to a key, with this:
|
50
53
|
|
51
54
|
```ruby
|
52
|
-
SimpleRedis.set('department-list',
|
55
|
+
SimpleRedis.set('department-list', Department.all)
|
53
56
|
```
|
54
57
|
|
55
58
|
### get
|
@@ -59,6 +62,19 @@ Or you can just get value of a key with this
|
|
59
62
|
SimpleRedis.get('department-list')
|
60
63
|
```
|
61
64
|
|
65
|
+
### delete_matched
|
66
|
+
This method remove a data based on key such as:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
SimpleRedis.delete_matched('department/fashion')
|
70
|
+
```
|
71
|
+
|
72
|
+
OR you can use regex
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
SimpleRedis.delete_matched('department/*')
|
76
|
+
```
|
77
|
+
|
62
78
|
## Configuration
|
63
79
|
|
64
80
|
Create `simple_redis.rb` in `config/initializers` folder
|
@@ -71,6 +87,14 @@ SimpleRedis.configuration do |config|
|
|
71
87
|
end
|
72
88
|
```
|
73
89
|
|
90
|
+
And you can change those 3 configurations on the fly, example:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
SimpleRedis.fetch(db: 'important-db', key: 'department-list', host: 'redis_host', port: 'redis_port') do
|
94
|
+
Department.all
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
74
98
|
## Development
|
75
99
|
|
76
100
|
## Contributing
|
data/lib/simple_redis/version.rb
CHANGED
data/lib/simple_redis.rb
CHANGED
@@ -13,17 +13,15 @@ module SimpleRedis
|
|
13
13
|
yield self
|
14
14
|
end
|
15
15
|
|
16
|
-
# Set or Get cache from Redis, opts => db, key, value, block
|
17
16
|
def self.fetch(opts={})
|
18
17
|
redis = get_redis(opts)
|
19
18
|
result = redis.get opts[:key]
|
20
|
-
result
|
21
|
-
get_result(result)
|
19
|
+
get_result(result || cache(redis, opts[:key], block_given? ? yield : opts[:value]))
|
22
20
|
end
|
23
21
|
|
24
22
|
def self.set(key, value, opts={})
|
25
23
|
redis = get_redis(opts)
|
26
|
-
result = cache(redis, key, value
|
24
|
+
result = cache(redis, key, value)
|
27
25
|
get_result(result)
|
28
26
|
end
|
29
27
|
|
@@ -45,24 +43,39 @@ module SimpleRedis
|
|
45
43
|
|
46
44
|
# PRIVATE METHODS
|
47
45
|
def self.cache(redis, key, value)
|
48
|
-
|
49
|
-
puts value
|
50
|
-
redis.set key, value
|
46
|
+
redis.set key, normalize(value)
|
51
47
|
value
|
52
48
|
end
|
53
49
|
|
50
|
+
def self.normalize(value)
|
51
|
+
value.class.in?(NORMAL_DATA_TYPES) ? value : value.to_json
|
52
|
+
end
|
53
|
+
|
54
54
|
def self.get_result(redis_result)
|
55
|
-
begin eval
|
55
|
+
begin eval(redis_result) rescue redis_result end
|
56
56
|
end
|
57
57
|
|
58
58
|
def self.get_redis(opts={})
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
current_opts.eql?(opts) ? current_redis : new_redis(opts)
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.new_redis(opts={})
|
63
|
+
current_opts = opts
|
64
|
+
current_redis = Redis.new(host: get_host(opts), port: get_port(opts), db: get_db(opts))
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.get_host(opts={})
|
68
|
+
opts[:host] || host || HOST
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.get_port(opts={})
|
72
|
+
opts[:port] || port || PORT
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.get_db(opts={})
|
76
|
+
opts[:db] || default_db || DEFAULT_DB
|
65
77
|
end
|
66
|
-
private_class_method :cache, :get_result, :get_redis
|
78
|
+
private_class_method :cache, :normalize, :get_result, :get_redis, :new_redis,
|
79
|
+
:get_host, :get_port, :get_db
|
67
80
|
|
68
81
|
end
|
Binary file
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# This file is auto-generated from the current state of the database. Instead
|
3
|
+
# of editing this file, please use the migrations feature of Active Record to
|
4
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
+
#
|
6
|
+
# Note that this schema.rb definition is the authoritative source for your
|
7
|
+
# database schema. If you need to create the application database on another
|
8
|
+
# system, you should be using db:schema:load, not running all the migrations
|
9
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
+
#
|
12
|
+
# It's strongly recommended that you check this file into your version control system.
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(version: 20160412222842) do
|
15
|
+
|
16
|
+
create_table "departments", force: :cascade do |t|
|
17
|
+
t.string "name"
|
18
|
+
t.datetime "created_at", null: false
|
19
|
+
t.datetime "updated_at", null: false
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
[1m[36m (1.4ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar NOT NULL) [0m
|
2
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
3
|
+
[1m[36m (1.3ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
4
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
5
|
+
Migrating to CreateDepartments (20160412222842)
|
6
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
7
|
+
[1m[35m (0.4ms)[0m CREATE TABLE "departments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
8
|
+
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES (?)[0m [["version", "20160412222842"]]
|
9
|
+
[1m[35m (1.7ms)[0m commit transaction
|
10
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
11
|
+
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
12
|
+
[1m[35mSQL (0.4ms)[0m INSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "Fashion"], ["created_at", "2016-04-12 22:30:46.307168"], ["updated_at", "2016-04-12 22:30:46.307168"]]
|
13
|
+
[1m[36m (1.2ms)[0m [1mcommit transaction[0m
|
14
|
+
[1m[35m (0.1ms)[0m begin transaction
|
15
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?)[0m [["name", "Electronic"], ["created_at", "2016-04-12 22:30:46.312783"], ["updated_at", "2016-04-12 22:30:46.312783"]]
|
16
|
+
[1m[35m (1.1ms)[0m commit transaction
|
17
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
18
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "Gadget"], ["created_at", "2016-04-12 22:30:46.315588"], ["updated_at", "2016-04-12 22:30:46.315588"]]
|
19
|
+
[1m[36m (1.5ms)[0m [1mcommit transaction[0m
|
20
|
+
[1m[35m (0.1ms)[0m begin transaction
|
21
|
+
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?)[0m [["name", "House"], ["created_at", "2016-04-12 22:30:46.318787"], ["updated_at", "2016-04-12 22:30:46.318787"]]
|
22
|
+
[1m[35m (0.9ms)[0m commit transaction
|
23
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
24
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "Event"], ["created_at", "2016-04-12 22:30:46.321500"], ["updated_at", "2016-04-12 22:30:46.321500"]]
|
25
|
+
[1m[36m (1.0ms)[0m [1mcommit transaction[0m
|
26
|
+
[1m[35m (0.1ms)[0m begin transaction
|
27
|
+
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?)[0m [["name", "Printing"], ["created_at", "2016-04-12 22:30:46.324276"], ["updated_at", "2016-04-12 22:30:46.324276"]]
|
28
|
+
[1m[35m (5.2ms)[0m commit transaction
|
29
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
30
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "Other"], ["created_at", "2016-04-12 22:30:46.331815"], ["updated_at", "2016-04-12 22:30:46.331815"]]
|
31
|
+
[1m[36m (1.0ms)[0m [1mcommit transaction[0m
|
32
|
+
[1m[35m (0.3ms)[0m SELECT COUNT(*) FROM "departments"
|
33
|
+
[1m[36mDepartment Load (1.1ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
34
|
+
[1m[35mDepartment Load (0.2ms)[0m SELECT "departments".* FROM "departments"
|
35
|
+
[1m[36mDepartment Load (1.1ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
36
|
+
[1m[36mDepartment Load (1.1ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
37
|
+
[1m[35mDepartment Load (0.3ms)[0m SELECT "departments".* FROM "departments"
|
38
|
+
[1m[36mDepartment Load (0.3ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
39
|
+
[1m[35mDepartment Load (0.3ms)[0m SELECT "departments".* FROM "departments"
|
40
|
+
[1m[36mDepartment Load (0.2ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
41
|
+
[1m[36mDepartment Load (5.9ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
42
|
+
[1m[36mDepartment Load (1.5ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
43
|
+
[1m[35mDepartment Load (0.3ms)[0m SELECT "departments".* FROM "departments"
|
44
|
+
[1m[36mDepartment Load (0.3ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
45
|
+
[1m[35mDepartment Load (0.2ms)[0m SELECT "departments".* FROM "departments"
|
46
|
+
[1m[36mDepartment Load (0.3ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
47
|
+
[1m[35mDepartment Load (0.2ms)[0m SELECT "departments".* FROM "departments"
|
48
|
+
[1m[36mDepartment Load (0.2ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
49
|
+
[1m[36mDepartment Load (6.0ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
50
|
+
[1m[35mDepartment Load (54.0ms)[0m SELECT "departments".* FROM "departments"
|
51
|
+
[1m[36mDepartment Load (0.3ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
52
|
+
[1m[35mDepartment Load (0.4ms)[0m SELECT "departments".* FROM "departments"
|
53
|
+
[1m[36mDepartment Load (0.3ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
54
|
+
[1m[35mDepartment Load (0.3ms)[0m SELECT "departments".* FROM "departments"
|
55
|
+
[1m[36mDepartment Load (0.3ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
56
|
+
[1m[35mDepartment Load (0.3ms)[0m SELECT "departments".* FROM "departments"
|
57
|
+
[1m[36mDepartment Load (0.3ms)[0m [1mSELECT "departments".* FROM "departments"[0m
|
58
|
+
[1m[35mDepartment Load (0.3ms)[0m SELECT "departments".* FROM "departments"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aditia Mahdar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- test/dummy/app/assets/stylesheets/application.css
|
72
72
|
- test/dummy/app/controllers/application_controller.rb
|
73
73
|
- test/dummy/app/helpers/application_helper.rb
|
74
|
+
- test/dummy/app/models/department.rb
|
74
75
|
- test/dummy/app/views/layouts/application.html.erb
|
75
76
|
- test/dummy/bin/bundle
|
76
77
|
- test/dummy/bin/rails
|
@@ -95,12 +96,16 @@ files:
|
|
95
96
|
- test/dummy/config/locales/en.yml
|
96
97
|
- test/dummy/config/routes.rb
|
97
98
|
- test/dummy/config/secrets.yml
|
98
|
-
- test/dummy/
|
99
|
+
- test/dummy/db/development.sqlite3
|
100
|
+
- test/dummy/db/migrate/20160412222842_create_departments.rb
|
101
|
+
- test/dummy/db/schema.rb
|
99
102
|
- test/dummy/log/development.log
|
100
103
|
- test/dummy/public/404.html
|
101
104
|
- test/dummy/public/422.html
|
102
105
|
- test/dummy/public/500.html
|
103
106
|
- test/dummy/public/favicon.ico
|
107
|
+
- test/dummy/test/fixtures/departments.yml
|
108
|
+
- test/dummy/test/models/department_test.rb
|
104
109
|
- test/simple_redis_test.rb
|
105
110
|
- test/test_helper.rb
|
106
111
|
homepage: https://github.com/aditiamahdar/simple_redis
|
@@ -132,6 +137,7 @@ test_files:
|
|
132
137
|
- test/dummy/app/assets/stylesheets/application.css
|
133
138
|
- test/dummy/app/controllers/application_controller.rb
|
134
139
|
- test/dummy/app/helpers/application_helper.rb
|
140
|
+
- test/dummy/app/models/department.rb
|
135
141
|
- test/dummy/app/views/layouts/application.html.erb
|
136
142
|
- test/dummy/bin/bundle
|
137
143
|
- test/dummy/bin/rails
|
@@ -156,7 +162,9 @@ test_files:
|
|
156
162
|
- test/dummy/config/routes.rb
|
157
163
|
- test/dummy/config/secrets.yml
|
158
164
|
- test/dummy/config.ru
|
159
|
-
- test/dummy/
|
165
|
+
- test/dummy/db/development.sqlite3
|
166
|
+
- test/dummy/db/migrate/20160412222842_create_departments.rb
|
167
|
+
- test/dummy/db/schema.rb
|
160
168
|
- test/dummy/log/development.log
|
161
169
|
- test/dummy/public/404.html
|
162
170
|
- test/dummy/public/422.html
|
@@ -164,5 +172,7 @@ test_files:
|
|
164
172
|
- test/dummy/public/favicon.ico
|
165
173
|
- test/dummy/Rakefile
|
166
174
|
- test/dummy/README.rdoc
|
175
|
+
- test/dummy/test/fixtures/departments.yml
|
176
|
+
- test/dummy/test/models/department_test.rb
|
167
177
|
- test/simple_redis_test.rb
|
168
178
|
- test/test_helper.rb
|
data/test/dummy/dump.rdb
DELETED
Binary file
|