millas 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5775286af7b48d9b93680c4d128276e5e74f7db
4
+ data.tar.gz: aa96ba109df7f26e791aa803a99c3ec0046f3f94
5
+ SHA512:
6
+ metadata.gz: 9497e7485318d25654645c183eb8b15a6920492476e9d7f1c5bf25537126ba3f9e31cf8c1efe574fd516658006e8ef869332fbe52730793939c2dc946e4aa039
7
+ data.tar.gz: ea44da53d1f8d4b74ca0836ba4aac8016a9afaf0f6b58b8471c5220a6d6f560525383ba6f441d62efd6ddb1e33ceff862c3f5ea7a0f8f5f859758ba01d2e9771
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Takuya Okuhara
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,88 @@
1
+ # Millas
2
+
3
+ Millas is a simple cache mechanism. In this mechanism, cached values are dispersed and layered in time.
4
+
5
+ By the way, Millas is a magic cake which becomes 3 layers when baked once. And it is traditional sweets in Landes (France).
6
+
7
+ ![compressed_thumbnail_square_small](https://user-images.githubusercontent.com/4189626/27506204-9d02d5f0-58ee-11e7-8517-8b16a003fd42.png)
8
+
9
+ ## Mechanism
10
+
11
+ ### Dispersed cache
12
+
13
+ Cached values whose key is appended random and specified number to the end.
14
+
15
+ ### Layered in time
16
+
17
+ Expiration time of cached values is Layered.
18
+
19
+ ## Usage
20
+
21
+ It is not conscious of Milas.
22
+
23
+ ```ruby
24
+ cache = ActiveSupport::Cache::DalliStore.new
25
+
26
+ cache.write('millas', 'cake', expires_in: 1.minutes)
27
+ # Cache write: millas-1 ({:expires_in=>60 seconds})
28
+ # Cache write: millas-2 ({:expires_in=>120 seconds})
29
+ # Cache write: millas-3 ({:expires_in=>180 seconds})
30
+ => 90916417477541888
31
+
32
+ cache.read('millas')
33
+ # Cache read: millas-2
34
+ => "cake"
35
+
36
+ cache.exist?('millas')
37
+ # Cache exist: millas-1
38
+ => true
39
+
40
+ cache.delete('millas')
41
+ # Cache delete: millas-1
42
+ # Cache delete: millas-2
43
+ # Cache delete: millas-3
44
+ => true
45
+ ```
46
+
47
+ ## Installation
48
+ Add this line to your application's Gemfile:
49
+
50
+ ```ruby
51
+ gem 'dalli'
52
+ gem 'millas'
53
+ ```
54
+
55
+ Or install it yourself as:
56
+ ```bash
57
+ $ gem install dalli
58
+ $ gem install millas
59
+ ```
60
+
61
+ ## Configuration
62
+
63
+ ```ruby
64
+ Millas.configure do |config|
65
+ config.dispersion_number = 5 # default
66
+ config.perform_dispersing = true # default
67
+ config.second_intervals = 60 # default
68
+ end
69
+ ```
70
+
71
+ If this mechanism is adopted only for a specific part, it should do as follows.
72
+
73
+ ```ruby
74
+ Millas.config.perform_dispersing = false
75
+
76
+ cache.write('millas', 'cake', expires_in: 1.minutes)
77
+ # Cache write: millas ({:expires_in=>60 seconds})
78
+ => 883549951894749184
79
+
80
+ cache.write('millas', 'cake', expires_in: 1.minutes, dispersed: true)
81
+ # Cache write: millas-1 ({:expires_in=>60 seconds})
82
+ # Cache write: millas-2 ({:expires_in=>120 seconds})
83
+ # Cache write: millas-3 ({:expires_in=>180 seconds})
84
+ => 15366844878541553664
85
+ ```
86
+
87
+ ## Copyright
88
+ Copyright (c) 2017 Takuya Okuhara. Licensed under the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module Millas
2
+ VERSION = '0.0.1'
3
+ end
data/lib/millas.rb ADDED
@@ -0,0 +1,99 @@
1
+ require "millas/version"
2
+ require 'active_support'
3
+ require 'active_support/cache/dalli_store'
4
+ require 'active_support/configurable'
5
+
6
+ module Millas
7
+ def self.configure(&block)
8
+ yield @config ||= Millas::Configuration.new
9
+ end
10
+
11
+ def self.config
12
+ @config
13
+ end
14
+
15
+ class Configuration
16
+ include ActiveSupport::Configurable
17
+ config_accessor :dispersion_number, :perform_dispersing, :second_intervals
18
+ end
19
+
20
+ configure do |config|
21
+ config.dispersion_number = 5
22
+ config.perform_dispersing = true
23
+ config.second_intervals = 60
24
+ end
25
+ end
26
+
27
+ class ActiveSupport::Cache::DalliStore
28
+ # monkey patch
29
+ alias_method :__read__, :read
30
+ alias_method :__write__, :write
31
+ alias_method :"__exist?__", :exist?
32
+ alias_method :__delete__, :delete
33
+ private :__read__, :__write__, :"__exist?__", :__delete__
34
+
35
+ def read(name, options = {})
36
+ dispersed = options.delete(:dispersed)
37
+ name = decorated_name(name) if dispersing?(dispersed)
38
+ __read__(name, options)
39
+ end
40
+
41
+ def write(name, value, options = {})
42
+ dispersed = options.delete(:dispersed)
43
+ if dispersing?(dispersed)
44
+ expires_in = options[:expires_in]
45
+ dispersions.map do |n|
46
+ options.merge!({ expires_in: advanced_expires_in(expires_in, n) }) if expires_in
47
+ __write__(decorated_name(name, n), value, options)
48
+ end.sample
49
+ else
50
+ __write__(name, value, options)
51
+ end
52
+ end
53
+
54
+ def exist?(name, options = {})
55
+ dispersed = options.delete(:dispersed)
56
+ name = decorated_name(name) if dispersing?(dispersed)
57
+ send("__exist?__", name, options)
58
+ end
59
+
60
+ def delete(name, options = {})
61
+ dispersed = options.delete(:dispersed)
62
+ if dispersing?(dispersed)
63
+ dispersions.map do |n|
64
+ __delete__(decorated_name(name, n), options)
65
+ end.sample
66
+ else
67
+ __delete__(name, options)
68
+ end
69
+ end
70
+
71
+ private
72
+ def dispersion_number
73
+ number = Millas.config.dispersion_number.try(:to_i)
74
+ number = 1 if !number || number <= 0
75
+ number
76
+ end
77
+
78
+ def dispersions
79
+ [*1..dispersion_number]
80
+ end
81
+
82
+ def dispersing?(dispersed)
83
+ b = Millas.config.perform_dispersing
84
+ b = !!dispersed unless dispersed.nil?
85
+ b
86
+ end
87
+
88
+ def second_intervals
89
+ (Millas.config.second_intervals || 0).seconds
90
+ end
91
+
92
+ def decorated_name(name, num = nil)
93
+ "#{name}-#{num || dispersions.sample}"
94
+ end
95
+
96
+ def advanced_expires_in(expires_in, n)
97
+ expires_in + second_intervals*(n-1)
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: millas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Takuya Okuhara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dalli
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple and dispersed cache mechanism.
70
+ email:
71
+ - work.okutaku0507@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/millas.rb
80
+ - lib/millas/version.rb
81
+ homepage: https://github.com/okutaku0507
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Simple and dispersed cache mechanism.
105
+ test_files: []