memoize_until 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +10 -0
- data/VERSION +1 -0
- data/examples/memoize_until.yml +7 -0
- data/lib/memoize_until.rb +5 -0
- data/lib/memoize_until/base.rb +50 -0
- data/lib/memoize_until/config/defaults.yml +10 -0
- data/lib/memoize_until/extensions.rb +12 -0
- data/lib/memoize_until/railtie.rb +14 -0
- data/lib/memoize_until/store.rb +65 -0
- data/lib/memoize_until/time_patch.rb +9 -0
- data/memoize_until.gemspec +37 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9f66521ac063cd6908fb61b5e05754a72a16beb3ba9862409f28b3b0beda85d1
|
4
|
+
data.tar.gz: 4749947d376dcb95804afffe1621da5a31c5a604904254b2c478800e47ea8602
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ceebecdc00e71e8618ad3404a1d85acc1e15cf3e9d5177aed0a8fd2258d8f9e8f3a7527c1bfaac5494a6f968febaaa7008c7e1a34a0dcf310f18abc0e43c029
|
7
|
+
data.tar.gz: 264f732011f7209d9254c6d0dd78f29b6f666467be328fd6d93ee5db03c4b123fa1f312a49882d58461e2d7fc462425e363ce271a56efb23d78d00531a751a10
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at ritikesh.g@freshdesk.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Ritikesh
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# MemoizeUntil
|
2
|
+
|
3
|
+
This gem is an extension to the standard `memoization` pattern for storing expensive computations or network calls `in-memory`. Unlike other memoization extensions which expire after a pre-defined interval, this gem provides a consistent memoization behavior across multiple processes/servers i.e. keys expire simultaneously across all processes.
|
4
|
+
|
5
|
+
Usage:
|
6
|
+
```ruby
|
7
|
+
gem install memoize_until
|
8
|
+
> irb
|
9
|
+
irb:> require 'memoize_until'
|
10
|
+
irb:> result = MemoizeUntil.day(:default) do
|
11
|
+
irb:> # PerformSomeComplexOperation
|
12
|
+
irb:> end # memoizes(until the end of the day) and returns the result of #PerformSomeComplexOperation
|
13
|
+
irb:> p result.inspect
|
14
|
+
```
|
15
|
+
|
16
|
+
The default API that the gem provides is: `MemoizeUntil#min, MemoizeUntil#hour, MemoizeUntil#day, MemoizeUntil#week, MemoizeUntil#month` with `default` purposes(keys).
|
17
|
+
|
18
|
+
To add new purposes during run_time, you can also leverage the `add_to` API:
|
19
|
+
```ruby
|
20
|
+
irb:> MemoizeUntil.add_to(:day, :runtime_key)
|
21
|
+
irb:> result = MemoizeUntil.day(:runtime_key) do
|
22
|
+
irb:> # PerformSomeComplexRuntimeOperation
|
23
|
+
irb:> end # memoizes(until the end of the day) and returns the result of #PerformSomeComplexOperation
|
24
|
+
irb:> p result.inspect
|
25
|
+
```
|
26
|
+
The same can be done for other default kinds as well: `min, hour, week, month`
|
27
|
+
|
28
|
+
## Rails
|
29
|
+
|
30
|
+
To use this gem in a rails application, add the following line to your `Gemfile` and you are good to go.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'memoize_until'
|
34
|
+
```
|
35
|
+
|
36
|
+
For most use cases, the list of purposes that come will not suffice. You can define your custom list of config purposes that you wish to memoize for, by including a `config/memoize_until.yml` in the root directory of your application. Here is an [example](/examples/memoize_until.yml) to help you with the file structure.
|
37
|
+
|
38
|
+
## Testing
|
39
|
+
To run test cases,
|
40
|
+
```shell
|
41
|
+
bundle install
|
42
|
+
ruby -Ilib:test test/memoize_until_test.rb
|
43
|
+
```
|
44
|
+
|
45
|
+
This project is Licensed under the MIT License. Further details can be found [here](/LICENSE).
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.0
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class MemoizeUntil
|
2
|
+
|
3
|
+
memoizable_attributes = YAML.load_file("#{__dir__}/config/defaults.yml")
|
4
|
+
|
5
|
+
if defined?(Rails) && File.exists?("#{Rails.root}/config/memoize_until.yml")
|
6
|
+
memoizable_attributes.deep_merge!(YAML.load_file("#{Rails.root}/config/memoize_until.yml"))
|
7
|
+
end
|
8
|
+
|
9
|
+
TYPE_FACTORY = {}
|
10
|
+
private_constant :TYPE_FACTORY
|
11
|
+
|
12
|
+
memoizable_attributes.each do |kind, keys|
|
13
|
+
|
14
|
+
TYPE_FACTORY[kind] = Store.new(kind)
|
15
|
+
|
16
|
+
keys.each { |key|
|
17
|
+
TYPE_FACTORY[kind].add(key)
|
18
|
+
}
|
19
|
+
|
20
|
+
# Memoizes a complex operation for the pre-specified interval
|
21
|
+
#
|
22
|
+
# @param key [Symbol] the purpose defined in memoize_until.yml or :default.
|
23
|
+
|
24
|
+
# @yield use this block to set the value to be memoized for the given moment
|
25
|
+
#
|
26
|
+
# @example Memoizing per day.
|
27
|
+
# MemoizeUntil.day(:default) do
|
28
|
+
# # other code
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# @return returns the memoized value for the key & moment
|
32
|
+
define_singleton_method(kind) do |key, &block|
|
33
|
+
TYPE_FACTORY[kind].fetch(key, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
# Adds a purpose at runtime for the specified kind.
|
39
|
+
#
|
40
|
+
# @param kind [Symbol] one of the default or customised kind supported
|
41
|
+
# @param key [Symbol] the purpose defined in memoize_until.yml or :default.
|
42
|
+
#
|
43
|
+
# @example Memoizing per day.
|
44
|
+
# MemoizeUntil.add_to(:day, :runtime_key)
|
45
|
+
#
|
46
|
+
# @return {}
|
47
|
+
def self.add_to(kind, key)
|
48
|
+
TYPE_FACTORY[kind].add(key)
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class MemoizeUntil::Railtie < Rails::Railtie
|
2
|
+
|
3
|
+
initializer "memoize_until.config_hook" do
|
4
|
+
# initialising the memoize_until classes
|
5
|
+
require 'memoize_until/base'
|
6
|
+
|
7
|
+
# require extensions
|
8
|
+
require 'memoize_until/extensions'
|
9
|
+
|
10
|
+
# patches
|
11
|
+
require 'memoize_until/time_patch'
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
class MemoizeUntil
|
2
|
+
class Store
|
3
|
+
attr_reader :_store, :_kind
|
4
|
+
|
5
|
+
def initialize(kind)
|
6
|
+
@_store = {}
|
7
|
+
@_kind = kind
|
8
|
+
end
|
9
|
+
|
10
|
+
# returns the value from memory if already memoized for "now" for the given key
|
11
|
+
# else evaluates the block and memoizes that
|
12
|
+
# caches nils too
|
13
|
+
def fetch(key, &block)
|
14
|
+
now = Time.now.public_send(_kind)
|
15
|
+
value = get(key, now)
|
16
|
+
unless value
|
17
|
+
clear_all(key)
|
18
|
+
value = set(key, now, yield)
|
19
|
+
end
|
20
|
+
unset_nil(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
# add runtime keys
|
24
|
+
def add(key)
|
25
|
+
_store[key] ||= {}
|
26
|
+
end
|
27
|
+
|
28
|
+
# clears all previously memoized values for the given key
|
29
|
+
# only clears memory in the process that this code runs.
|
30
|
+
# added for fetch and custom scripts
|
31
|
+
def clear_all(key)
|
32
|
+
_store[key] = {}
|
33
|
+
end
|
34
|
+
|
35
|
+
# clears previously memoized value for "now" for the given key
|
36
|
+
# only clears memory in the process that this code runs.
|
37
|
+
# added for custom scripts
|
38
|
+
def clear_now(key)
|
39
|
+
now = Time.now.public_send(_kind)
|
40
|
+
_store[key][now] = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# caches nils through a pseudo object
|
46
|
+
def set_nil(value)
|
47
|
+
value.nil? ? NullObject.instance : value
|
48
|
+
end
|
49
|
+
|
50
|
+
# replaces cached pseudo object and returns nil
|
51
|
+
def unset_nil(value)
|
52
|
+
value.is_a?(NullObject) ? nil : value
|
53
|
+
end
|
54
|
+
|
55
|
+
def set(key, now, value)
|
56
|
+
_store[key][now] = set_nil(value)
|
57
|
+
end
|
58
|
+
|
59
|
+
def get(key, now)
|
60
|
+
purpose = _store[key]
|
61
|
+
raise NotImplementedError unless purpose
|
62
|
+
purpose[now]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
version = File.read(File.expand_path("VERSION", __dir__)).strip
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "memoize_until"
|
8
|
+
spec.version = version
|
9
|
+
spec.authors = ["Ritikesh"]
|
10
|
+
spec.email = ["ritikesh.ganpathraj@freshworks.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Local Memoization Pattern!}
|
13
|
+
spec.description = %q{Local Memoization Pattern to store complex and repeated computations in memory until the next hour/day/week}
|
14
|
+
spec.homepage = "https://github.com/freshdesk/memoize_until"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = "https://github.com/freshdesk/memoize_until"
|
24
|
+
else
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
26
|
+
end
|
27
|
+
|
28
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
29
|
+
f.match(%r{^(test|spec|features)/})
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency 'concurrent-ruby', '~> 1.0'
|
36
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memoize_until
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ritikesh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: concurrent-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
description: Local Memoization Pattern to store complex and repeated computations
|
42
|
+
in memory until the next hour/day/week
|
43
|
+
email:
|
44
|
+
- ritikesh.ganpathraj@freshworks.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- CODE_OF_CONDUCT.md
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- VERSION
|
56
|
+
- examples/memoize_until.yml
|
57
|
+
- lib/memoize_until.rb
|
58
|
+
- lib/memoize_until/base.rb
|
59
|
+
- lib/memoize_until/config/defaults.yml
|
60
|
+
- lib/memoize_until/extensions.rb
|
61
|
+
- lib/memoize_until/railtie.rb
|
62
|
+
- lib/memoize_until/store.rb
|
63
|
+
- lib/memoize_until/time_patch.rb
|
64
|
+
- memoize_until.gemspec
|
65
|
+
homepage: https://github.com/freshdesk/memoize_until
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata:
|
69
|
+
allowed_push_host: https://rubygems.org
|
70
|
+
homepage_uri: https://github.com/freshdesk/memoize_until
|
71
|
+
source_code_uri: https://github.com/freshdesk/memoize_until
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubygems_version: 3.0.6
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Local Memoization Pattern!
|
91
|
+
test_files: []
|