Bläddra i källkod

Enable redis in production and optionally in dev env

Andrew Swistak 6 år sedan
förälder
incheckning
56081f010f

+ 2 - 0
.env.template

@@ -17,6 +17,8 @@ POSTGRES_PASSWORD=postgres
 # will be created as `ENV['POSTGRES_DB'] + "_test"`
 POSTGRES_DB=pokemon_trade_dev
 
+REDIS_URL=127.0.0.1:6379
+
 # Port which Rails is exposed on, _only used for docker containers_.
 WEB_HOST_PORT=3000
 

+ 1 - 2
Gemfile

@@ -13,12 +13,11 @@ gem 'haml'
 gem 'pg'
 gem 'puma', '~> 4.2'
 gem 'rails', '~> 6.0.0'
+gem 'redis'
 gem 'rest-client', require: false
 gem 'sass-rails', '~> 6.0'
 gem 'webpacker'
 
-# Use Redis adapter to run Action Cable in production
-# gem 'redis', '~> 4.0'
 # Use Active Model has_secure_password
 # gem 'bcrypt', '~> 3.1.7'
 # Use Active Storage variant

+ 2 - 0
Gemfile.lock

@@ -205,6 +205,7 @@ GEM
     rb-fsevent (0.10.3)
     rb-inotify (0.10.0)
       ffi (~> 1.0)
+    redis (4.1.3)
     regexp_parser (1.6.0)
     rest-client (2.1.0)
       http-accept (>= 1.7.0, < 2.0)
@@ -320,6 +321,7 @@ DEPENDENCIES
   puma (~> 4.2)
   rails (~> 6.0.0)
   rails-controller-testing
+  redis
   rest-client
   rspec-rails!
   rubocop (~> 0.75.0)

+ 6 - 1
config/environments/development.rb

@@ -21,7 +21,12 @@ Rails.application.configure do
     config.action_controller.perform_caching = true
     config.action_controller.enable_fragment_cache_logging = true
 
-    config.cache_store = :memory_store
+    config.cache_store = if ENV['REDIS_URL']
+      [:redis_cache_store, {url: ENV['REDIS_URL']}]
+    else
+      :memory_store
+    end
+
     config.public_file_server.headers = {
       'Cache-Control' => "public, max-age=#{2.days.to_i}",
     }

+ 1 - 1
config/environments/production.rb

@@ -66,7 +66,7 @@ Rails.application.configure do
   config.log_tags = [:request_id]
 
   # Use a different cache store in production.
-  # config.cache_store = :mem_cache_store
+  config.cache_store = :redis_cache_store, {url: ENV['REDIS_URL']}
 
   # Use a real queuing backend for Active Job (and separate queues per
   # environment)

+ 5 - 0
config/initializers/session_store.rb

@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+# Be sure to restart your server when you modify this file.
+Rails.application.config.session_store :cache_store,
+                                       key: '_pokemon_trade_session'

+ 6 - 0
docker-compose.yml

@@ -31,6 +31,7 @@ services:
       - POSTGRES_PORT=5432
       - POSTGRES_USER
       - WEBPACKER_DEV_SERVER_HOST=webpack_dev_server
+      - REDIS_URL=redis://redis:6379
     ports:
       - ${WEB_HOST_PORT}:3000
 
@@ -46,6 +47,11 @@ services:
     <<: *app
     command: yarn run relay --watch
 
+  redis:
+    image: redis:5.0.7-alpine
+    ports:
+      - ${REDIS_HOST_PORT}:6379
+
   db:
     image: postgres:11.2-alpine
     environment: