Skip to content
Browse files

Add support for Redis as a DB backend

  • Loading branch information...
1 parent 4f34c90 commit 25e07e704fe2403d15921614876f04fa37034bf7 @halorgium halorgium committed Nov 7, 2012
Showing with 48 additions and 15 deletions.
  1. +1 −0 Gemfile
  2. +3 −1 Gemfile.lock
  3. +1 −0 app/libraries.rb
  4. +41 −10 app/models/db.rb
  5. +2 −4 app/views/stats.rb
View
1 Gemfile
@@ -2,6 +2,7 @@ source :rubygems
gem 'sinatra', '~>1.0'
gem 'yajl-ruby', '~>1.1.0'
+gem 'redis'
gem 'mustache', '~>0.11.2'
gem 'curb', '~>0.7.8'
gem 'coderay', '~>0.8.357'
View
4 Gemfile.lock
@@ -7,7 +7,7 @@ GEM
faraday (0.8.4)
multipart-post (~> 1.1)
faraday_middleware (0.8.8)
- faraday (< 0.9, >= 0.7.4)
+ faraday (>= 0.7.4, < 0.9)
hashie (1.2.0)
json (1.7.3)
mime-types (1.16)
@@ -24,6 +24,7 @@ GEM
hashie (~> 1.2)
multi_json (~> 1.3)
rack (1.2.1)
+ redis (3.0.2)
rest-client (1.6.1)
mime-types (>= 1.16)
shotgun (0.8)
@@ -53,6 +54,7 @@ DEPENDENCIES
coderay (~> 0.8.357)
curb (~> 0.7.8)
mustache (~> 0.11.2)
+ redis
shotgun
sinatra (~> 1.0)
sinatra_auth_github (~> 0.10.0)
View
1 app/libraries.rb
@@ -12,6 +12,7 @@
# bundled gems
require 'sinatra/base'
require 'yajl'
+require 'redis'
require 'curb'
require 'mustache/sinatra'
require 'sinatra/auth/github'
View
51 app/models/db.rb
@@ -1,7 +1,34 @@
require 'fileutils'
module Hurl
- class DB
+ class AbstractDB
+ def self.encode(object)
+ Zlib::Deflate.deflate Yajl::Encoder.encode(object)
+ end
+
+ def self.decode(object)
+ Yajl::Parser.parse(Zlib::Inflate.inflate(object)) rescue nil
+ end
+ end
+
+ class RedisDB < AbstractDB
+ uri = URI.parse(ENV.fetch("REDISTOGO_URL", "redis://127.0.0.1:6379"))
+ CONNECTION = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
+
+ def self.find(scope, id)
+ decode(CONNECTION.get("hurl/#{scope}/#{id}"))
+ end
+
+ def self.save(scope, id, content)
+ CONNECTION.set("hurl/#{scope}/#{id}", encode(content))
+ end
+
+ def self.count(scope)
+ CONNECTION.keys("hurl/#{scope}/*").size
+ end
+ end
+
+ class FileDB < AbstractDB
DIR = File.expand_path(ENV['HURL_DB_DIR'] || "db")
def self.find(scope, id)
@@ -18,6 +45,13 @@ def self.save(scope, id, content)
true
end
+ def self.count(scope)
+ files = Dir["#{DIR}/#{scope}/**/**"].reject do |file|
+ File.directory?(file)
+ end
+ files.size
+ end
+
def self.dir(scope, id)
path = FileUtils.mkdir_p "#{DIR}/#{scope}/#{id[0...2]}/#{id[2...4]}/"
@@ -29,14 +63,11 @@ def self.dir(scope, id)
path
end
end
-
- private
- def self.encode(object)
- Zlib::Deflate.deflate Yajl::Encoder.encode(object)
- end
-
- def self.decode(object)
- Yajl::Parser.parse(Zlib::Inflate.inflate(object)) rescue nil
- end
end
+
+ db_backend = ENV.fetch("DB_BACKEND", "file")
+ DB = {
+ "file" => FileDB,
+ "redis" => RedisDB,
+ }.fetch(db_backend)
end
View
6 app/views/stats.rb
@@ -9,11 +9,9 @@ def hurl_stats
end
def count(thing)
- files = Dir["#{Hurl::DB::DIR}/#{thing}/**/**"].reject do |file|
- File.directory?(file)
- end
+ count = Hurl::DB.count(thing)
- { :stat => thing, :value => files.size }
+ { :stat => thing, :value => count }
end
def disk_stats

0 comments on commit 25e07e7

Please sign in to comment.
Something went wrong with that request. Please try again.