custom_cache 0.0.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +30 -0
- data/Rakefile +32 -0
- data/lib/custom_cache/acts_as_custom_cache.rb +24 -0
- data/lib/custom_cache/base.rb +5 -0
- data/lib/custom_cache/middleware.rb +16 -0
- data/lib/custom_cache/railtie.rb +9 -0
- data/lib/custom_cache/request_cache.rb +42 -0
- data/lib/custom_cache/scope_cache.rb +7 -0
- data/lib/custom_cache/session_cache.rb +42 -0
- data/lib/custom_cache/version.rb +3 -0
- data/lib/custom_cache.rb +9 -0
- data/lib/tasks/custom_cache_tasks.rake +4 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a8b1f13ab73f777a1f7347da60918f75d8767917
|
4
|
+
data.tar.gz: 1026ed87191cd16bc1486a2a1cf3661bc7b18822
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa25fc5d180681ae5d5c83c3d55c7dddabde23598de169342da9c9fe67601c3e9403e1477fc144d0d86ee2ea65edc02a7f18f8a33e3c732f3bc0a39f8c3bb7b1
|
7
|
+
data.tar.gz: e2ffe8b9194ec23b743e6eadd5bfd79df37c47db6e7d25191fdc134de9369deb6f9bd26d15adfebe2be6927ac36a1956852a49038afba12f4ea11d358f77c4ce
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Chirag Viradiya
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# custom_cache
|
2
|
+
|
3
|
+
A wrapper over Rails cache to provide request and session cache
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'custom_cache'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install custom_cache
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The request_cache can be used to share information over the request between controller, models etc without re-fetching it
|
22
|
+
|
23
|
+
Rails.request_cache.write('current_product_id', 'd4f30996-4dba-4805-8114-6b35d1272858')
|
24
|
+
Rails.request_cache.read('current_product_id')
|
25
|
+
|
26
|
+
The session_cache can be used to share information across the requests between controller, models etc without re-fetching it
|
27
|
+
|
28
|
+
Rails.session_cache.write('capabilities', ['REPORTS_VIEW', 'PRODUCT_CRUD'])
|
29
|
+
Rails.session_cache.read('capabilities')
|
30
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'CustomCache'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CustomCache
|
2
|
+
module ActsAsCustomCache
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def session_cache
|
13
|
+
CustomCache::SessionCache.instance
|
14
|
+
end
|
15
|
+
|
16
|
+
def request_cache
|
17
|
+
CustomCache::RequestCache.instance
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
Rails.send :include, CustomCache::ActsAsCustomCache
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module CustomCache
|
2
|
+
class Middleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
request = ActionDispatch::Request.new(env)
|
9
|
+
CustomCache::RequestCache.create(request.uuid)
|
10
|
+
CustomCache::SessionCache.create(request.session.id)
|
11
|
+
@app.call(env)
|
12
|
+
ensure
|
13
|
+
CustomCache::RequestCache.clear!
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module CustomCache
|
2
|
+
|
3
|
+
class RequestCache < Base
|
4
|
+
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
attr_accessor :request_id
|
8
|
+
|
9
|
+
def self.create(request_id)
|
10
|
+
self.instance.request_id = request_id
|
11
|
+
end
|
12
|
+
|
13
|
+
def cache_key
|
14
|
+
"_request_#{request_id}_cache" if request_id.present?
|
15
|
+
end
|
16
|
+
|
17
|
+
def read(key)
|
18
|
+
if cache_key.present?
|
19
|
+
content = Rails.cache.read(cache_key)
|
20
|
+
content[key] if content.present?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def write(key, content)
|
25
|
+
if cache_key.present?
|
26
|
+
cached_content = Rails.cache.read(cache_key) || {}
|
27
|
+
cached_content[key] = content
|
28
|
+
Rails.cache.write(cache_key, cached_content)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def clear
|
33
|
+
Rails.cache.delete(cache_key) if cache_key.present?
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.clear!
|
37
|
+
self.instance.clear
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module CustomCache
|
2
|
+
|
3
|
+
class SessionCache < Base
|
4
|
+
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
attr_accessor :session_id
|
8
|
+
|
9
|
+
def self.create(session_id)
|
10
|
+
self.instance.session_id = session_id
|
11
|
+
end
|
12
|
+
|
13
|
+
def cache_key
|
14
|
+
"_session_#{session_id}_cache" if session_id.present?
|
15
|
+
end
|
16
|
+
|
17
|
+
def read(key)
|
18
|
+
if cache_key.present?
|
19
|
+
content = Rails.cache.read(cache_key)
|
20
|
+
content[key] if content.present?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def write(key, content)
|
25
|
+
if cache_key.present?
|
26
|
+
cached_content = Rails.cache.read(cache_key) || {}
|
27
|
+
cached_content[key] = content
|
28
|
+
Rails.cache.write(cache_key, cached_content)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def clear
|
33
|
+
Rails.cache.delete(cache_key) if cache_key.present?
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.clear!
|
37
|
+
self.instance.clear
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/custom_cache.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'custom_cache/acts_as_custom_cache'
|
2
|
+
require 'custom_cache/base'
|
3
|
+
require 'custom_cache/request_cache'
|
4
|
+
require 'custom_cache/session_cache'
|
5
|
+
require 'custom_cache/middleware'
|
6
|
+
require 'custom_cache/railtie' if defined?(Rails::Railtie)
|
7
|
+
|
8
|
+
module CustomCache
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: custom_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chirag Viradiya
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.10
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.10
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A wrapper over Rails cache to provide request and session cache.
|
42
|
+
email:
|
43
|
+
- chirag.viradiya@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/custom_cache.rb
|
52
|
+
- lib/custom_cache/acts_as_custom_cache.rb
|
53
|
+
- lib/custom_cache/base.rb
|
54
|
+
- lib/custom_cache/middleware.rb
|
55
|
+
- lib/custom_cache/railtie.rb
|
56
|
+
- lib/custom_cache/request_cache.rb
|
57
|
+
- lib/custom_cache/scope_cache.rb
|
58
|
+
- lib/custom_cache/session_cache.rb
|
59
|
+
- lib/custom_cache/version.rb
|
60
|
+
- lib/tasks/custom_cache_tasks.rake
|
61
|
+
homepage: http://rubygems.org/gems/custom_cache
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.2.2
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: A wrapper over Rails cache to provide request and session cache.
|
85
|
+
test_files: []
|
86
|
+
has_rdoc:
|