Redis caching with docker-compose
Set up caching in redis with local support for docker-compose
Used 46 times
T
TobiasBales
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/Xo5sNe"
Template Source
Review the code before running this template on your machine.
def file_contains?(filename, string)
File.foreach(filename).detect { |line| line.include?(string) }
end
gem 'redis' unless file_contains? 'Gemfile', "Gem 'redis'"
gem 'hiredis' unless file_contains? 'Gemfile', "Gem 'hiredis'"
Bundler.with_unbundled_env { run 'bundle install' }
unless File.exist? 'docker-compose.yml'
create_file 'docker-compose.yml' do <<~YAML
version: "3.1"
services:
YAML
end
end
inject_into_file 'docker-compose.yml' do <<-YAML
redis-cache:
image: redis
restart: always
volumes:
- ./tmp/redis-cache:/data
ports:
- 6379:6379
YAML
end
application do <<~EOF
config.cache_store = :redis_cache_store, {url: ENV["REDIS_URL"]}
EOF
end