Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,43 +110,45 @@ end

namespace :cache do
task :config do # rubocop:disable Rake/Desc
require_relative 'config/environment' # load config info
require_relative 'app/infrastructure/cache/local_cache'
require_relative 'app/infrastructure/cache/redis_cache'
require_relative 'config/environment' # load config info
@api = CodePraise::App
end

desc 'Directory listing of local dev cache'
namespace :list do
desc 'Lists development cache'
task :dev do
task :dev => :config do
puts 'Lists development cache'
list = `ls _cache/rack/meta`
puts 'No local cache found' if list.empty?
puts list
keys = CodePraise::Cache::Local.new(@api.config).keys
puts 'No local cache found' if keys.none?
keys.each { |key| puts "Key: #{key}" }
end

desc 'Lists production cache'
task :production => :config do
puts 'Finding production cache'
keys = CodePraise::Cache::Client.new(@api.config).keys
keys = CodePraise::Cache::Remote.new(@api.config).keys
puts 'No keys found' if keys.none?
keys.each { |key| puts "Key: #{key}" }
end
end

namespace :wipe do
desc 'Delete development cache'
task :dev do
task :dev => :config do
puts 'Deleting development cache'
sh 'rm -rf _cache/*'
CodePraise::Cache::Local.new(@api.config).wipe
puts 'Development cache wiped'
end

desc 'Delete production cache'
task :production => :config do
print 'Are you sure you wish to wipe the production cache? (y/n) '
if $stdin.gets.chomp.downcase == 'y'
puts 'Deleting production cache'
wiped = CodePraise::Cache::Client.new(@api.config).wipe
wiped = CodePraise::Cache::Remote.new(@api.config).wipe
wiped.each { |key| puts "Wiped: #{key}" }
end
end
Expand Down
29 changes: 29 additions & 0 deletions app/infrastructure/cache/local_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'fileutils'

module CodePraise
module Cache
# Local disk cache utility
class Local
def initialize(config)
@cache_dir = config.LOCAL_CACHE
ensure_cache_directory
end

def keys
Dir.glob("#{@cache_dir}/**/*").select { |f| File.file?(f) }
end

def wipe
FileUtils.rm_rf(Dir.glob("#{@cache_dir}/*"))
end

private

def ensure_cache_directory
FileUtils.mkdir_p(@cache_dir)
end
end
end
end
2 changes: 1 addition & 1 deletion app/infrastructure/cache/redis_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module CodePraise
module Cache
# Redis client utility
class Client
class Remote
def initialize(config)
@redis = Redis.new(url: config.REDISCLOUD_URL)
end
Expand Down
4 changes: 2 additions & 2 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def self.config = Figaro.env
configure :development do
use Rack::Cache,
verbose: true,
metastore: 'file:_cache/rack/meta',
entitystore: 'file:_cache/rack/body'
metastore: "#{config.LOCAL_CACHE}/meta",
entitystore: "#{config.LOCAL_CACHE}/body"
end

configure :production do
Expand Down
3 changes: 3 additions & 0 deletions config/secrets_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ development:
DB_FILENAME: db/local/dev.db
GITHUB_TOKEN: create_github_token
REPOSTORE_PATH: repostore
LOCAL_CACHE: _cache/rack
API_HOST: http://localhost:9090
REDISCLOUD_URL: url-assigned-by-Redis-provider-on-Heroku

app_test:
DB_FILENAME: db/local/test.db
REPOSTORE_PATH: repostore
LOCAL_CACHE: _cache/rack
API_HOST: http://localhost:9090
REDISCLOUD_URL: url-assigned-by-Redis-provider-on-Heroku

test:
DB_FILENAME: db/local/test.db
GITHUB_TOKEN: create_github_token
REPOSTORE_PATH: repostore
LOCAL_CACHE: _cache/rack
API_HOST: http://localhost:9090
REDISCLOUD_URL: url-assigned-by-Redis-provider-on-Heroku

Expand Down
Loading