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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 087f723bd9d7ac56465f4c284f551799bccea348
4
- data.tar.gz: c133b2b7edad763eb3973ca05740314bdd344285
3
+ metadata.gz: d8f4c4a270c4100fa724e81a2388641ce7451bb6
4
+ data.tar.gz: d6bac06fd91939654889fef9c61f2285aff7e92f
5
5
  SHA512:
6
- metadata.gz: fb7da3df82067becd4d644453598b4e58b88f1ee4162bfbf0e3cd23157c7fee540e054f89f86adb2c6fd12507901167cfbce16a47bbc4076fc268c181c6d4d5b
7
- data.tar.gz: c4d55a64cd5369ce0ececc7ac3813c4c4705a06116564fa276ace7cf3f2c5bacd18f9aec7af32cff86ba3f761ff1cc85540f9eb8bf73e8b5916d17fbe99f24a6
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: departments)
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', departments)
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
@@ -1,6 +1,7 @@
1
1
  module SimpleRedis
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  HOST = "localhost"
4
4
  PORT = 6379
5
5
  DEFAULT_DB = "0"
6
+ NORMAL_DATA_TYPES = [String, Fixnum, Integer, Float, NilClass, Symbol, Range]
6
7
  end
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 = cache(redis, opts[:key], block_given? ? yield.inspect : opts[:value].inspect) if result.nil?
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.inspect)
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
- puts key
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 redis_result rescue redis_result end
55
+ begin eval(redis_result) rescue redis_result end
56
56
  end
57
57
 
58
58
  def self.get_redis(opts={})
59
- if current_opts.eql? opts
60
- self.current_redis
61
- else
62
- self.current_opts = opts
63
- self.current_redis = Redis.new(host: host || HOST, port: port || PORT, db: opts[:db] || default_db || DEFAULT_DB)
64
- end
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
@@ -0,0 +1,2 @@
1
+ class Department < ActiveRecord::Base
2
+ end
Binary file
@@ -0,0 +1,9 @@
1
+ class CreateDepartments < ActiveRecord::Migration
2
+ def change
3
+ create_table :departments do |t|
4
+ t.string :name
5
+
6
+ t.timestamps null: false
7
+ end
8
+ end
9
+ end
@@ -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
+  (1.4ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) 
2
+  (0.1ms) select sqlite_version(*)
3
+  (1.3ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
4
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
5
+ Migrating to CreateDepartments (20160412222842)
6
+  (0.1ms) begin transaction
7
+  (0.4ms) CREATE TABLE "departments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
8
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160412222842"]]
9
+  (1.7ms) commit transaction
10
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
11
+  (0.2ms) begin transaction
12
+ SQL (0.4ms) 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
+  (1.2ms) commit transaction
14
+  (0.1ms) begin transaction
15
+ SQL (0.2ms) INSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "Electronic"], ["created_at", "2016-04-12 22:30:46.312783"], ["updated_at", "2016-04-12 22:30:46.312783"]]
16
+  (1.1ms) commit transaction
17
+  (0.1ms) begin transaction
18
+ SQL (0.2ms) 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
+  (1.5ms) commit transaction
20
+  (0.1ms) begin transaction
21
+ SQL (0.3ms) INSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "House"], ["created_at", "2016-04-12 22:30:46.318787"], ["updated_at", "2016-04-12 22:30:46.318787"]]
22
+  (0.9ms) commit transaction
23
+  (0.1ms) begin transaction
24
+ SQL (0.2ms) 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
+  (1.0ms) commit transaction
26
+  (0.1ms) begin transaction
27
+ SQL (0.3ms) INSERT INTO "departments" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "Printing"], ["created_at", "2016-04-12 22:30:46.324276"], ["updated_at", "2016-04-12 22:30:46.324276"]]
28
+  (5.2ms) commit transaction
29
+  (0.1ms) begin transaction
30
+ SQL (0.3ms) 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
+  (1.0ms) commit transaction
32
+  (0.3ms) SELECT COUNT(*) FROM "departments"
33
+ Department Load (1.1ms) SELECT "departments".* FROM "departments"
34
+ Department Load (0.2ms) SELECT "departments".* FROM "departments"
35
+ Department Load (1.1ms) SELECT "departments".* FROM "departments"
36
+ Department Load (1.1ms) SELECT "departments".* FROM "departments"
37
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
38
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
39
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
40
+ Department Load (0.2ms) SELECT "departments".* FROM "departments"
41
+ Department Load (5.9ms) SELECT "departments".* FROM "departments"
42
+ Department Load (1.5ms) SELECT "departments".* FROM "departments"
43
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
44
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
45
+ Department Load (0.2ms) SELECT "departments".* FROM "departments"
46
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
47
+ Department Load (0.2ms) SELECT "departments".* FROM "departments"
48
+ Department Load (0.2ms) SELECT "departments".* FROM "departments"
49
+ Department Load (6.0ms) SELECT "departments".* FROM "departments"
50
+ Department Load (54.0ms) SELECT "departments".* FROM "departments"
51
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
52
+ Department Load (0.4ms) SELECT "departments".* FROM "departments"
53
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
54
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
55
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
56
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
57
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
58
+ Department Load (0.3ms) SELECT "departments".* FROM "departments"
@@ -0,0 +1,7 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+ one:
4
+ name: MyString
5
+
6
+ two:
7
+ name: MyString
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class DepartmentTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
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.2.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-10 00:00:00.000000000 Z
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/dump.rdb
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/dump.rdb
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