restrict_cache 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/restrict_cache/accessible.rb +16 -0
- data/lib/restrict_cache/action_controller_ext.rb +21 -0
- data/lib/restrict_cache/active_record_ext.rb +53 -0
- data/lib/restrict_cache/cache_sweeper.rb +14 -0
- data/lib/restrict_cache/cacheable/active_record_cache.rb +20 -0
- data/lib/restrict_cache/cacheable/base.rb +22 -0
- data/lib/restrict_cache/cacheable/custom_cache.rb +13 -0
- data/lib/restrict_cache/cacheable.rb +55 -0
- data/lib/restrict_cache/railtie.rb +17 -0
- data/lib/restrict_cache/version.rb +3 -0
- data/lib/restrict_cache.rb +6 -0
- data/restrict_cache.gemspec +25 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e4a37b2b0b86aaf69fda3297713e32fdd1951722
|
4
|
+
data.tar.gz: f221ff5b19e8c16e2844d0e45e70aa1989226ac8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a80b5f4e4070bf06a1bd4ec645c1d05f320f5b69dce451779c524677168b5578dab87744061d127502185cad5171677f7a8775164f7ffec1a4df1053ba0a8cda
|
7
|
+
data.tar.gz: 2833d2da4631d6873c4e7b5fc8ba3a5f6809850d7f78a0e12e1432851a5752740e3c9969fb47ccb5f6328a43bbe4f3178b211488ff6d6928c416d3c7870adec7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 i2bskn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# RestrictCache
|
2
|
+
|
3
|
+
Restricted cache in the rack request.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'restrict_cache'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install restrict_cache
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Using the middleware or callbacks to clear the cache for each request.
|
24
|
+
|
25
|
+
#### Rack Middleware
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
module MyApp
|
29
|
+
class Application < Rails::Application
|
30
|
+
config.middleware.use RestrictCache::CacheSweeper
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
#### Callbacks (Around Action)
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class MyModel < ActiveRecord::Base; end
|
39
|
+
MyModel.find_with_restrict_cache(id_or_ids)
|
40
|
+
```
|
41
|
+
|
42
|
+
## Development
|
43
|
+
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
45
|
+
|
46
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( https://github.com/i2bskn/restrict_cache/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create a new Pull Request
|
55
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "restrict_cache"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
class ActionControllerExt
|
3
|
+
include ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def cache_sweep_action(*args)
|
7
|
+
options = args.extract_options!
|
8
|
+
actions = args.empty? ? {} : {only: args}
|
9
|
+
options.merge! actions unless actions.empty?
|
10
|
+
around_action :sweep_restrict_cache, options
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def sweep_restrict_cache
|
15
|
+
yield
|
16
|
+
ensure
|
17
|
+
RestrictCache.clear
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
module ActiveRecordExt
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def find_and_restrict_cache(arg)
|
9
|
+
records = find(arg)
|
10
|
+
Array(records).each(&:restrict_cache)
|
11
|
+
records
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_from_restrict_cache(arg)
|
15
|
+
contents = RestrictCache.collection.active_record.contents(self.table_name)
|
16
|
+
return nil unless contents
|
17
|
+
|
18
|
+
case arg
|
19
|
+
when Integer
|
20
|
+
contents[arg]
|
21
|
+
when String
|
22
|
+
contents[arg.to_i]
|
23
|
+
when Array
|
24
|
+
args.map {|index| contents[index.to_i] }
|
25
|
+
else
|
26
|
+
raise "unknown argument: #{arg.inspect}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def find_with_restrict_cache(arg)
|
31
|
+
if restrict_cached?(Array(arg))
|
32
|
+
records = Array(arg).map {|index| find_from_restrict_cache(index) }
|
33
|
+
records.size > 1 ? records : records.first
|
34
|
+
else
|
35
|
+
find_and_restrict_cache(arg)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def restrict_cached?(args)
|
41
|
+
content = RestrictCache.collection.active_record.contents(self.table_name)
|
42
|
+
return false unless content
|
43
|
+
ids = content.keys
|
44
|
+
args.all? {|index| ids.include?(index) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def restrict_cache
|
49
|
+
RestrictCache.collection.add(self)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
class Cacheable
|
3
|
+
class ActiveRecordCache < Base
|
4
|
+
def add(content)
|
5
|
+
@caches[table_name(content)] ||= {}
|
6
|
+
@caches[table_name(content)][content.id] = content
|
7
|
+
end
|
8
|
+
|
9
|
+
def contents(_table_name)
|
10
|
+
@caches[_table_name.to_sym]
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def table_name(content)
|
15
|
+
content.class.table_name.to_sym
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
class Cacheable
|
3
|
+
class Base
|
4
|
+
def initialize
|
5
|
+
@caches = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(content)
|
9
|
+
raise NotImplementedError
|
10
|
+
end
|
11
|
+
|
12
|
+
def contents(_table_name)
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
def size
|
17
|
+
@caches.size
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "restrict_cache/cacheable/base"
|
2
|
+
require "restrict_cache/cacheable/active_record_cache"
|
3
|
+
require "restrict_cache/cacheable/custom_cache"
|
4
|
+
|
5
|
+
module RestrictCache
|
6
|
+
class Cacheable
|
7
|
+
module CacheKey
|
8
|
+
ACTIVERECORD = :active_record_cache
|
9
|
+
CUSTOM = :custom_cache
|
10
|
+
ALL = [ACTIVERECORD, CUSTOM]
|
11
|
+
|
12
|
+
def self.get(content)
|
13
|
+
if defined?(ActiveRecord) && content.class < ActiveRecord::Base
|
14
|
+
return ACTIVERECORD
|
15
|
+
end
|
16
|
+
CUSTOM
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def build_inner_cache(key)
|
22
|
+
const_get(key.to_s.classify, false).new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
CacheKey::ALL.each do |inner_cache_key|
|
27
|
+
define_method inner_cache_key do
|
28
|
+
inner_cache inner_cache_key
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize
|
33
|
+
@cache_collection = {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def add(content)
|
37
|
+
inner_cache(CacheKey.get(content)).add(content)
|
38
|
+
end
|
39
|
+
|
40
|
+
def [](key)
|
41
|
+
return nil unless key
|
42
|
+
@cache_collection[key]
|
43
|
+
end
|
44
|
+
|
45
|
+
def cache_keys
|
46
|
+
CacheKey::ALL
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def inner_cache(key)
|
51
|
+
@cache_collection[key] ||= self.class.build_inner_cache(key)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer "restrict_cache" do
|
4
|
+
require "restrict_cache/action_controller_ext"
|
5
|
+
require "restrict_cache/active_record_ext"
|
6
|
+
|
7
|
+
ActiveSupport.on_load(:action_controller) do
|
8
|
+
::ActionController::Base.send(:include, RestrictCache::ActionControllerExt)
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveSupport.on_load(:active_record) do
|
12
|
+
::ActiveRecord::Base.send(:include, RestrictCache::ActiveRecordExt)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'restrict_cache/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "restrict_cache"
|
7
|
+
spec.version = RestrictCache::VERSION
|
8
|
+
spec.authors = ["i2bskn"]
|
9
|
+
spec.email = ["i2bskn@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Restricted cache in the rack request.}
|
12
|
+
spec.description = %q{Restricted cache in the rack request.}
|
13
|
+
spec.homepage = "https://github.com/i2bskn/restrict_cache"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
25
|
+
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restrict_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- i2bskn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Restricted cache in the rack request.
|
56
|
+
email:
|
57
|
+
- i2bskn@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/restrict_cache.rb
|
73
|
+
- lib/restrict_cache/accessible.rb
|
74
|
+
- lib/restrict_cache/action_controller_ext.rb
|
75
|
+
- lib/restrict_cache/active_record_ext.rb
|
76
|
+
- lib/restrict_cache/cache_sweeper.rb
|
77
|
+
- lib/restrict_cache/cacheable.rb
|
78
|
+
- lib/restrict_cache/cacheable/active_record_cache.rb
|
79
|
+
- lib/restrict_cache/cacheable/base.rb
|
80
|
+
- lib/restrict_cache/cacheable/custom_cache.rb
|
81
|
+
- lib/restrict_cache/railtie.rb
|
82
|
+
- lib/restrict_cache/version.rb
|
83
|
+
- restrict_cache.gemspec
|
84
|
+
homepage: https://github.com/i2bskn/restrict_cache
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.4.5
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Restricted cache in the rack request.
|
108
|
+
test_files: []
|