cache_ninja 1.0.1 → 1.0.2
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 +4 -4
- data/README.md +196 -13
- data/cache_ninja.gemspec +3 -3
- data/lib/cache_ninja/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83ee0d146451ed55161a7795238620d250c1bece1017c023d336a88df8163afd
|
4
|
+
data.tar.gz: 13d66c99105d6a7f2b1b2f5da25b1aa15f90cb5722ad6b7aef598e39c5e29866
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7005546d5daeef01c91a9e43bcad86ff7a8f8a2fb2c7956644bfc5e4beb9ab33ce8f13be18e23281b22cf29c45c217b1c0bda8cdbd808a58848992961cc15f5
|
7
|
+
data.tar.gz: 687ad171c94c9d57a91856ebf6e736f3dfa632f5ad08af745abd382cc670af512c5c11399a4f12b17b690a3bda75fa5138a8d24f84679e57d5a4e7ba50470571
|
data/README.md
CHANGED
@@ -1,34 +1,216 @@
|
|
1
|
-
#
|
1
|
+
# Cache Ninja
|
2
|
+
[](https://opensource.org/licenses/MIT)
|
3
|
+
[](https://rubygems.org/gems/cache_ninja)
|
4
|
+
[](https://badge.fury.io/rb/cache_ninja)
|
2
5
|
|
3
|
-
|
6
|
+
**Cache Ninja** is the ultimate solution for safely and efficiently caching associations in your Ruby on Rails applications. While association caching is typically discouraged, there are specific scenarios where it's not just beneficial but essential for optimizing performance. **Cache Ninja** has been rigorously tested and is currently employed in our production-grade servers, making it the most reliable gem for association caching in Rails.
|
4
7
|
|
5
|
-
|
8
|
+
- [Use Cases](#use-cases)
|
9
|
+
- [Installation](#installation)
|
10
|
+
- [Getting Started](#getting-started)
|
11
|
+
- [Usage](#usage)
|
12
|
+
- [Cache Objects](#cache-objects)
|
13
|
+
- [Cache Associations](#cache-associations)
|
14
|
+
- [Cache Multiple Associations](#cache-multiple-associations)
|
15
|
+
- [No need to worry about Cyclicity](#no-need-to-worry-about-cyclicity)
|
16
|
+
- [Cache Associations with Options](#cache-associations-with-options)
|
17
|
+
- [Using Cache Ninja in your existing Rails Application](#using-cache-ninja-in-your-existing-rails-application)
|
18
|
+
- [Contributing](#contributing)
|
19
|
+
- [License](#license)
|
20
|
+
|
21
|
+
## Use Cases
|
22
|
+
In the world of Ruby on Rails, association caching has often been viewed with caution. However, there are use cases where association caching can be a game-changer, and that's where **Cache Ninja** comes into play. Here are some examples of when association caching can be beneficial:
|
23
|
+
|
24
|
+
* **Focus on Business Logic**:
|
25
|
+
Caching associations with **Cache Ninja** means you can shift your focus from low-level optimization to what truly matters: your application's business logic. It empowers developers to streamline their code and deliver better user experiences without worrying about performance bottlenecks.
|
26
|
+
|
27
|
+
* **Tangible Performance Boost**:
|
28
|
+
In our own experience, implementing **Cache Ninja** led to a remarkable 5x reduction in latency for heavily used models. This kind of performance enhancement can be a game-changer for applications that require lightning-fast responses.
|
29
|
+
|
30
|
+
* **Effortless Integration**:
|
31
|
+
**Cache Ninja** seamlessly integrates into your Rails application, ensuring that you can start reaping the benefits of association caching without unnecessary complexity. The gem is well-documented, making it easy to get started and unlock its potential.
|
32
|
+
|
33
|
+
* **Safe and Proven in Production**:
|
34
|
+
**Cache Ninja** is not just another gem; it's a battle-tested solution that powers our internal production-grade servers. We've put it through its paces to ensure that it delivers the utmost in performance and reliability. When it comes to caching associations, **Cache Ninja** is the gem you can trust.
|
6
35
|
|
7
36
|
## Installation
|
8
37
|
|
9
|
-
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
gem 'cache_ninja'
|
42
|
+
```
|
10
43
|
|
11
|
-
|
44
|
+
```shell
|
45
|
+
bundle install
|
46
|
+
```
|
12
47
|
|
13
|
-
|
48
|
+
Or install it yourself as:
|
14
49
|
|
15
|
-
|
50
|
+
```shell
|
51
|
+
gem install cache_ninja
|
52
|
+
```
|
16
53
|
|
17
|
-
|
54
|
+
## Getting Started
|
55
|
+
|
56
|
+
After installing Cache Ninja, you need to include it in your models. To get started, add the following line to your model class:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class User < ApplicationRecord
|
60
|
+
include CacheNinja
|
61
|
+
|
62
|
+
# ...
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
**Cache Ninja** is now ready to use in your model.
|
18
67
|
|
19
68
|
## Usage
|
20
69
|
|
21
|
-
|
70
|
+
**NOTE: Any changes made to an object, removes its data and the data of its associated objects from the cache - This is done to ensure that the cached data is always up-to-date and consistent.**
|
71
|
+
|
72
|
+
### Cache Objects:
|
73
|
+
Wherever you include **CacheNinja**, it will dynamically create `fetch_cached()` method on the instances of that particular model (it takes id as parameter). You can use it as such:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class User < ApplicationRecord
|
77
|
+
include CacheNinja
|
78
|
+
|
79
|
+
# ...
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
user = User.fetch_cached(1)
|
85
|
+
```
|
86
|
+
|
87
|
+
### Cache Associations:
|
88
|
+
Once Cache Ninja is included in your model, you can start caching associations using the `cache_assoc` method. Here's an example:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
class User < ApplicationRecord
|
92
|
+
include CacheNinja
|
93
|
+
|
94
|
+
# Cache the 'teams' association
|
95
|
+
cache_assoc :teams
|
96
|
+
|
97
|
+
# ...
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
This will dynamically create a method `cached_teams` which can be then used for reducing database queries and improving performance. Now you can access the cached association i.e. `cached_teams` data in this case, as such:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
user = User.first
|
105
|
+
user.cached_teams
|
106
|
+
```
|
107
|
+
|
108
|
+
### Cache Multiple Associations:
|
109
|
+
You can also cache multiple associations at once:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class User < ApplicationRecord
|
113
|
+
include CacheNinja
|
114
|
+
|
115
|
+
# Cache the 'teams' and 'posts' associations
|
116
|
+
cache_assoc :teams, :posts
|
117
|
+
|
118
|
+
# ...
|
119
|
+
end
|
120
|
+
```
|
22
121
|
|
23
|
-
|
122
|
+
This will dynamically create the methods `cached_teams`, `cached_posts` which can be then used for reducing database queries and improving performance. Now you can access the cached associations i.e. `cached_teams`, `cached_posts` data in this case, as such:
|
24
123
|
|
25
|
-
|
124
|
+
```ruby
|
125
|
+
user = User.first
|
126
|
+
user.cached_teams
|
127
|
+
user.cached_posts
|
128
|
+
```
|
26
129
|
|
27
|
-
|
130
|
+
### No need to worry about Cyclicity:
|
131
|
+
Cache Ninja takes care of cyclicity in associations. For example, if you have a `User` model which has many `Posts` and each `Post` belongs to a `User`, then you can cache both associations without worrying about cyclicity. Here's an example:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
class User < ApplicationRecord
|
135
|
+
include CacheNinja
|
136
|
+
|
137
|
+
# Cache the 'posts' and 'comments' associations
|
138
|
+
cache_assoc :posts, :comments
|
139
|
+
|
140
|
+
# ...
|
141
|
+
end
|
142
|
+
|
143
|
+
class Post < ApplicationRecord
|
144
|
+
include CacheNinja
|
145
|
+
|
146
|
+
# Cache the 'user' association
|
147
|
+
cache_assoc :user
|
148
|
+
|
149
|
+
# ...
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
### Cache Associations with Options:
|
154
|
+
Suppose for the above example you want to cache user's posts but not cache user data for a particular post. You can do that by passing options to `cache_assoc` method. Here's an example:
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
class User < ApplicationRecord
|
158
|
+
include CacheNinja
|
159
|
+
|
160
|
+
# Cache the 'posts' and 'comments' associations
|
161
|
+
cache_assoc :posts, :comments
|
162
|
+
|
163
|
+
# ...
|
164
|
+
end
|
165
|
+
|
166
|
+
class Post < ApplicationRecord
|
167
|
+
include CacheNinja
|
168
|
+
|
169
|
+
# Cache the 'user' association
|
170
|
+
cache_assoc :user, disable: true
|
171
|
+
|
172
|
+
# ...
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
## Using Cache Ninja in your existing Rails Application
|
177
|
+
|
178
|
+
1. Once you have installed Cache Ninja, you can include it in `ApplicationRecord` class as such:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
class ApplicationRecord < ActiveRecord::Base
|
182
|
+
self.abstract_class = true
|
183
|
+
|
184
|
+
include CacheNinja
|
185
|
+
end
|
186
|
+
```
|
187
|
+
|
188
|
+
2. Now you can start caching associations in your models as such:
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
class User < ApplicationRecord
|
192
|
+
# Cache the 'teams' association
|
193
|
+
cache_assoc :teams
|
194
|
+
|
195
|
+
# ...
|
196
|
+
end
|
197
|
+
```
|
198
|
+
|
199
|
+
3. Now search for the instances in your application where you are using something similar to `user.teams` and replace it with `user.cached_teams` as such:
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
def index
|
203
|
+
@users = User.all
|
204
|
+
@teams = @users.collect(&:cached_teams)
|
205
|
+
end
|
206
|
+
```
|
207
|
+
|
208
|
+
NOTE: This gem is not intended to be a replacement for solving *n+1 queries*. It is intended to be used in scenarios where you have a lot of associations and have no option but to cache them in order to improve performance.
|
28
209
|
|
29
210
|
## Contributing
|
211
|
+
In case of use cases not covered by this gem, please feel free to open an issue or a pull request. You can even write to `arjun.verma8412@gmail.com`. We would love to hear your feedback and suggestions.
|
30
212
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
213
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/arjun9/cache_ninja. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/arjun9/cache_ninja/blob/master/CODE_OF_CONDUCT.md).
|
32
214
|
|
33
215
|
## License
|
34
216
|
|
@@ -37,3 +219,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
37
219
|
## Code of Conduct
|
38
220
|
|
39
221
|
Everyone interacting in the CacheNinja project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cache_ninja/blob/master/CODE_OF_CONDUCT.md).
|
222
|
+
|
data/cache_ninja.gemspec
CHANGED
@@ -10,15 +10,15 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = "In the world of Ruby on Rails, association caching has often been viewed with caution. However, there are use cases where association caching can be a game-changer, and that's where **Cache Ninja** comes into play."
|
12
12
|
spec.description = "**Cache Ninja** is the ultimate solution for safely and efficiently caching associations in your Ruby on Rails applications. While association caching is typically discouraged, there are specific scenarios where it's not just beneficial but essential for optimizing performance. **Cache Ninja** has been rigorously tested and is currently employed in our production-grade servers, making it the most reliable gem for association caching in Rails."
|
13
|
-
spec.homepage = "https://github.com/
|
13
|
+
spec.homepage = "https://github.com/arjun9/cache_ninja"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 2.6.0"
|
16
16
|
|
17
17
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
18
|
|
19
19
|
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
-
spec.metadata["source_code_uri"] = "https://github.com/
|
21
|
-
spec.metadata["changelog_uri"] = "https://github.com/
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/arjun9/cache_ninja"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/arjun9/cache_ninja/blob/main/CHANGELOG.md"
|
22
22
|
|
23
23
|
# Specify which files should be added to the gem when it is released.
|
24
24
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
data/lib/cache_ninja/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_ninja
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arjun Verma
|
@@ -48,14 +48,14 @@ files:
|
|
48
48
|
- lib/cache_ninja/faceless_obj.rb
|
49
49
|
- lib/cache_ninja/version.rb
|
50
50
|
- sig/cache_ninja.rbs
|
51
|
-
homepage: https://github.com/
|
51
|
+
homepage: https://github.com/arjun9/cache_ninja
|
52
52
|
licenses:
|
53
53
|
- MIT
|
54
54
|
metadata:
|
55
55
|
allowed_push_host: https://rubygems.org
|
56
|
-
homepage_uri: https://github.com/
|
57
|
-
source_code_uri: https://github.com/
|
58
|
-
changelog_uri: https://github.com/
|
56
|
+
homepage_uri: https://github.com/arjun9/cache_ninja
|
57
|
+
source_code_uri: https://github.com/arjun9/cache_ninja
|
58
|
+
changelog_uri: https://github.com/arjun9/cache_ninja/blob/main/CHANGELOG.md
|
59
59
|
post_install_message:
|
60
60
|
rdoc_options: []
|
61
61
|
require_paths:
|