evanescence 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: a6f2dca734bfb023b9f2c927e49904ca5c34c4247ec619ce704d766ad01f8d66
4
- data.tar.gz: c336919947cecc092f7d8e1c4495e4f46097d65bc92ed446c36b64b7c90dbe0c
3
+ metadata.gz: f68d8f11de5eae7b6526d8b4b040a6645507f18419baf1ffedae2606b122ed08
4
+ data.tar.gz: 33df762bf878be62d629983e20a8df54206f1fb9842854e7da8571e9ae52ad6e
5
5
  SHA512:
6
- metadata.gz: 5dd994c61a90b4932917924e347866ba92d60d93431ebc559848e1c43adde7c99bd3e50d6fceb348fc70a837573bc8e8a0a937c991e66afc86fce5c6c70b5f39
7
- data.tar.gz: 12ab15620128dda67b0b53480bb7ddea55a0f11769aac037a3b4f3e98c2cb07e7a2e01fab75ce34a0d931f3ea6617ee0176c46ab5fe82a6d8c2876bbc328fc68
6
+ metadata.gz: 9905ccbb185e07c001ad293199435970f67398cd7c937b388fcdf265eea112c77b4ac1d88f5cc7214fb0fe8f2ab9980faba2dafc46f40e002c6496ca92fa0ea8
7
+ data.tar.gz: aa2325ccfea0b558bc2518b3359dc537b1ef11a0467d09920f2d1ae90971cadc453582322e6c8a1d765774a3596e41cc44af9a3cfdf7c3477c0981e2e6341f68
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Evanescence
2
2
 
3
3
  [![Push & PR](https://github.com/florindiconescu/evanescence/actions/workflows/main.yml/badge.svg)](https://github.com/florindiconescu/evanescence/actions/workflows/main.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/evanescence.svg)](https://badge.fury.io/rb/evanescence)
4
5
 
5
6
  In memory cache which can limit the cache size by a maximum number by expiring the least used cached items.
6
7
 
@@ -20,6 +21,34 @@ Delete the entire cache: `cache.clear`
20
21
 
21
22
  Check the cache size: `cache.count`
22
23
 
24
+ ### In rails with activerecords:
25
+
26
+ Add `gem "evanescence"` to your gemfile.
27
+
28
+ In your `ApplicationRecord`:
29
+ ```
30
+ require "evanescence/cacheable"
31
+
32
+ class ApplicationRecord < ActiveRecord::Base
33
+ extend Evanescence::Cacheable
34
+ ...
35
+ end
36
+ ```
37
+
38
+ In your model class:
39
+ ```
40
+ class Article < ApplicationRecord
41
+ caches by: :id, max_size: 1000
42
+ ...
43
+ end
44
+ ```
45
+
46
+ In your controller:
47
+ ```
48
+ Article.get_by(params[:id])
49
+ ```
50
+
51
+
23
52
  ## Next steps
24
53
  Expire the cache after given time.
25
54
 
@@ -0,0 +1,21 @@
1
+ module Evanescence
2
+ module Cacheable
3
+ def caches(options = {})
4
+ @by = options[:by]
5
+ @max_size = options[:max_size]
6
+ @cache = Evanescence.initialize_cache(max_size:)
7
+ end
8
+
9
+ def get_by(key)
10
+ cache.read(key) || send("find_by_#{by}", key)&.tap { |object| write_by(key, object) }
11
+ end
12
+
13
+ def write_by(key, value)
14
+ cache.write(key, value)
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :by, :cache, :max_size
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Evanescence
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evanescence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - florindiconescu
@@ -66,6 +66,7 @@ files:
66
66
  - evanescence.gemspec
67
67
  - lib/evanescence.rb
68
68
  - lib/evanescence/cache.rb
69
+ - lib/evanescence/cacheable.rb
69
70
  - lib/evanescence/version.rb
70
71
  - sig/evanescence.rbs
71
72
  homepage: https://github.com/florindiconescu/evanescence
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
93
  - !ruby/object:Gem::Version
93
94
  version: '0'
94
95
  requirements: []
95
- rubygems_version: 3.5.3
96
+ rubygems_version: 3.5.5
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: Simple in memory cache