Skip to content

Commit 2e0b6d4

Browse files
committed
Merge branch 'feature/cache-redis' into develop
* feature/cache-redis: Added redis docker run command for cache Deleted config.cache_store that not working Added how to redis Added PostHelper Added pagaination_param for redis key Added redis namespece docker run redis Added after_save :clear_cache in Post Added fetch_categories in categories controller Changed redis port to 7001 Added to_json https://www.sitepoint.com/rails-model-caching-redis/
2 parents 7078be7 + e2ce9ab commit 2e0b6d4

File tree

10 files changed

+195
-19
lines changed

10 files changed

+195
-19
lines changed

Gemfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ gem 'kaminari'
6666

6767
gem 'lograge'
6868
gem 'logstash-event'
69-
gem 'logstash-logger'
69+
gem 'logstash-logger'
70+
71+
gem 'redis'
72+
gem 'redis-namespace'
73+
gem 'redis-rails'
74+
gem 'redis-rack-cache'

Gemfile.lock

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ GEM
138138
puma (4.3.1)
139139
nio4r (~> 2.0)
140140
rack (2.0.8)
141+
rack-cache (1.11.0)
142+
rack (>= 0.4)
141143
rack-test (1.1.0)
142144
rack (>= 1.0, < 3)
143145
rails (6.0.2.1)
@@ -170,6 +172,28 @@ GEM
170172
rb-fsevent (0.10.3)
171173
rb-inotify (0.10.1)
172174
ffi (~> 1.0)
175+
redis (4.1.3)
176+
redis-actionpack (5.2.0)
177+
actionpack (>= 5, < 7)
178+
redis-rack (>= 2.1.0, < 3)
179+
redis-store (>= 1.1.0, < 2)
180+
redis-activesupport (5.2.0)
181+
activesupport (>= 3, < 7)
182+
redis-store (>= 1.3, < 2)
183+
redis-namespace (1.7.0)
184+
redis (>= 3.0.4)
185+
redis-rack (2.1.0)
186+
rack (>= 2.0.8, < 3)
187+
redis-store (>= 1.2, < 2)
188+
redis-rack-cache (2.2.1)
189+
rack-cache (>= 1.10, < 2)
190+
redis-store (>= 1.6, < 2)
191+
redis-rails (5.0.2)
192+
redis-actionpack (>= 5.0, < 6)
193+
redis-activesupport (>= 5.0, < 6)
194+
redis-store (>= 1.2, < 2)
195+
redis-store (1.8.1)
196+
redis (>= 4, < 5)
173197
request_store (1.5.0)
174198
rack (>= 1.4)
175199
rspec-core (3.9.1)
@@ -241,6 +265,10 @@ DEPENDENCIES
241265
pg (>= 0.18, < 2.0)
242266
puma (~> 4.1)
243267
rails (~> 6.0.2, >= 6.0.2.1)
268+
redis
269+
redis-namespace
270+
redis-rack-cache
271+
redis-rails
244272
rspec-rails (~> 3.5)
245273
rswag-api
246274
rswag-specs

README.md

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@ How to Run
44
----------
55

66
1. Ready to database
7-
```bash
8-
bundle install
9-
````
10-
```bash
11-
rake docker:db:init
12-
rake docker:db:run
13-
```
14-
```bash
15-
rake db:migrate
16-
rake db:migrate RAILS_ENV=test
17-
````
18-
```bash
19-
rake db:seed
20-
```
7+
1. Run Postgresql and migrate
8+
```bash
9+
bundle install
10+
````
11+
```bash
12+
rake docker:db:init
13+
rake docker:db:run
14+
```
15+
```bash
16+
rake db:migrate
17+
rake db:migrate RAILS_ENV=test
18+
````
19+
```bash
20+
rake db:seed
21+
```
22+
2. Run Redis for cache
23+
```bash
24+
docker run --rm --name my-redis-container -p 7001:6379 -d redis redis-server --appendonly yes
25+
redis-cli -h localhost -p 7001
26+
```
2127
2. ELK
2228
```bash
2329
git clone https://github.com/deviantony/docker-elk
@@ -87,6 +93,11 @@ How what to do
8793
* [Log For ELK stack (Elastic Search, Logstash, Kibana)](#log-for-elk-stack-elastic-search-logstash-kibana)
8894
* [lograge.rb with custom config](#logragerb-with-custom-config)
8995
* [ELK Setup](/rails_log_with_elk_setup.md)
96+
* [Redis](#redis)
97+
* [server run](#server-run)
98+
* [add gem](#add-gem)
99+
* [config](#config)
100+
* [how to added cache](#how-to-added-cache)
90101

91102
### Build Json with active_model_serializers Gem
92103
1. Gemfile
@@ -317,3 +328,74 @@ https://ericlondon.com/2017/01/26/integrate-rails-logs-with-elasticsearch-logsta
317328
```
318329
319330
#### [ELK Setup](/rails_log_with_elk_setup.md)
331+
332+
### Redis
333+
#### server run
334+
```bash
335+
docker run --rm --name my-redis-container -p 7001:6379 -d redis redis-server --appendonly yes
336+
redis-cli -h localhost -p 7001
337+
```
338+
339+
#### add gem
340+
```ruby
341+
gem 'redis'
342+
gem 'redis-namespace'
343+
gem 'redis-rails'
344+
gem 'redis-rack-cache'
345+
```
346+
347+
#### config
348+
```ruby
349+
# config/initializers/redis.rb
350+
351+
$redis = Redis::Namespace.new("tutorial_post", :redis => Redis.new(:host => '127.0.0.1', :port => 7001))
352+
353+
```
354+
355+
#### how to added cache
356+
```ruby
357+
# GET /categories
358+
def index
359+
page = params[:page].present? ? params[:page] : 1
360+
per = params[:per].present? ? params[:per] : 10
361+
pagaination_param = {
362+
category_page: page,
363+
category_per: per,
364+
post_page: @post_page,
365+
post_per: @post_per
366+
}
367+
@categories = fetch_categories pagaination_param
368+
render json: @categories
369+
end
370+
```
371+
372+
```ruby
373+
class Category < ApplicationRecord
374+
include CategoryHelper
375+
...your code
376+
after_save :clear_cache
377+
end
378+
```
379+
380+
```ruby
381+
# app/helpers/category_helper.rb
382+
module CategoryHelper
383+
def fetch_categories pagaination_param
384+
page = pagaination_param[:category_page]
385+
per = pagaination_param[:category_per]
386+
key = "categories"+pagaination_param.to_s
387+
categories = $redis.get(key)
388+
if categories.nil?
389+
@categories = Category.published.by_date.page(page).per(per)
390+
categories = Pagination.build_json(@categories, pagaination_param).to_json
391+
$redis.set(key, categories)
392+
$redis.expire(key, 1.hour.to_i)
393+
end
394+
categories
395+
end
396+
def clear_cache
397+
keys = $redis.keys "*categories*"
398+
keys.each {|key| $redis.del key}
399+
end
400+
end
401+
```

app/controllers/categories_controller.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class CategoriesController < ApplicationController
2+
include CategoryHelper
23
before_action :authorize_request
34
before_action :post_pagination_params, only: [:index, :show]
45
before_action except: [:index, :show] do
@@ -13,12 +14,14 @@ class CategoriesController < ApplicationController
1314
def index
1415
page = params[:page].present? ? params[:page] : 1
1516
per = params[:per].present? ? params[:per] : 10
16-
@categories = Category.published.by_date.page(page).per(per)
1717
pagaination_param = {
18+
category_page: page,
19+
category_per: per,
1820
post_page: @post_page,
1921
post_per: @post_per
2022
}
21-
render json: Pagination.build_json(@categories, pagaination_param)
23+
@categories = fetch_categories pagaination_param
24+
render json: @categories
2225
end
2326

2427
# GET /categories/1

app/controllers/posts_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class PostsController < ApplicationController
2+
include PostHelper
23
before_action :authorize_request
34
before_action :comment_pagination_params, only: [:index, :show]
45
before_action :set_post, only: [:show, :update, :destroy]
@@ -13,12 +14,16 @@ def index
1314
page = params[:page].present? ? params[:page] : 1
1415
per = params[:per].present? ? params[:per] : 10
1516
@category = Category.published.find(category_id) if category_id.present?
16-
@posts = Post.category(category_id).search(search).published.by_date.page(page).per(per)
1717
pagaination_param = {
18+
category_id: category_id,
19+
search: search,
20+
post_page: page,
21+
post_per: per,
1822
comment_page: @comment_page,
1923
comment_per: @comment_per
2024
}
21-
render json: Pagination.build_json(@posts, pagaination_param)
25+
@posts = fetch_posts pagaination_param
26+
render json: @posts
2227
end
2328

2429
# GET /posts/1

app/helpers/category_helper.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# app/helpers/category_helper.rb
2+
3+
module CategoryHelper
4+
def fetch_categories pagaination_param
5+
page = pagaination_param[:category_page]
6+
per = pagaination_param[:category_per]
7+
key = "categories"+pagaination_param.to_s
8+
categories = $redis.get(key)
9+
if categories.nil?
10+
@categories = Category.published.by_date.page(page).per(per)
11+
categories = Pagination.build_json(@categories, pagaination_param).to_json
12+
$redis.set(key, categories)
13+
$redis.expire(key, 1.hour.to_i)
14+
end
15+
categories
16+
end
17+
def clear_cache
18+
keys = $redis.keys "*categories*"
19+
keys.each {|key| $redis.del key}
20+
end
21+
end

app/helpers/post_helper.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# app/helpers/post_helper.rb
2+
3+
module PostHelper
4+
def fetch_posts pagaination_param
5+
category_id = pagaination_param[:category_id]
6+
search = pagaination_param[:search]
7+
page = pagaination_param[:post_page]
8+
per = pagaination_param[:post_per]
9+
key = "posts"+pagaination_param.to_s
10+
posts = $redis.get(key)
11+
if posts.nil?
12+
@posts = Post.category(category_id).search(search).published.by_date.page(page).per(per)
13+
posts = Pagination.build_json(@posts, pagaination_param).to_json
14+
$redis.set(key, posts)
15+
$redis.expire(key, 1.hour.to_i)
16+
end
17+
posts
18+
end
19+
def clear_cache_posts
20+
keys = $redis.keys "*posts*"
21+
keys.each {|key| $redis.del key}
22+
end
23+
end

app/models/category.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class Category < ApplicationRecord
2+
include CategoryHelper
23
belongs_to :user
34
has_many :posts
45
scope :published, -> { where(published: true) }
56
scope :by_date, -> { order('created_at DESC, id DESC') }
67
validates :title, presence: true
78
validates :body, presence: true
9+
after_save :clear_cache
810
end

app/models/post.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Post < ApplicationRecord
2+
include CategoryHelper
3+
include PostHelper
24
belongs_to :category
35
belongs_to :user
46
has_many :comments
@@ -7,4 +9,6 @@ class Post < ApplicationRecord
79
scope :published, -> { where(published: true) }
810
scope :by_date, -> { order('created_at DESC, id DESC') }
911
validates :body, presence: true
12+
after_save :clear_cache
13+
after_save :clear_cache_posts
1014
end

config/initializers/redis.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# config/initializers/redis.rb
2+
3+
$redis = Redis::Namespace.new("tutorial_post", :redis => Redis.new(:host => '127.0.0.1', :port => 7001))

0 commit comments

Comments
 (0)