implemented Redis functionality

This commit is contained in:
Thomas Buchöster
2011-06-10 02:12:43 +02:00
parent 0991dd1aa2
commit 61de00cad7
14 changed files with 171 additions and 36 deletions
+1
View File
@@ -19,6 +19,7 @@ gem 'typhoeus'
# for object caching
gem 'redis'
gem 'redis-namespace'
# for using 7digital api
gem '7digital'
+11 -1
View File
@@ -33,8 +33,10 @@ GEM
activesupport (3.0.5)
addressable (2.2.6)
arel (2.0.10)
bcrypt-ruby (2.1.4)
bcrypt-ruby (2.1.4-x86-mingw32)
builder (2.1.2)
daemons (1.0.10)
devise (1.3.4)
bcrypt-ruby (~> 2.1.2)
orm_adapter (~> 0.0.3)
@@ -44,7 +46,7 @@ GEM
faraday (0.6.1)
addressable (~> 2.2.4)
multipart-post (~> 1.1.0)
rack (>= 1.1.0, < 2)
rack (< 2, >= 1.1.0)
gem_plugin (0.2.3)
haml (3.1.2)
haml-rails (0.3.4)
@@ -64,6 +66,9 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mongrel (1.2.0.pre2)
daemons (~> 1.0.10)
gem_plugin (~> 0.2.3)
mongrel (1.2.0.pre2-x86-mingw32)
gem_plugin (~> 0.2.3)
multi_json (1.0.3)
@@ -114,6 +119,8 @@ GEM
thor (~> 0.14.4)
rake (0.9.2)
redis (2.2.1)
redis-namespace (1.0.3)
redis (< 3.0.0)
ruby-openid (2.1.8)
ruby-openid-apps-discovery (1.2.0)
ruby-openid (>= 2.1.7)
@@ -121,6 +128,7 @@ GEM
sexp_processor (~> 3.0)
sass (3.1.2)
sexp_processor (3.0.5)
sqlite3 (1.3.3)
sqlite3 (1.3.3-x86-mingw32)
sqlite3-ruby (1.3.3)
sqlite3 (>= 1.3.3)
@@ -139,6 +147,7 @@ GEM
oauth (>= 0.4.4)
PLATFORMS
ruby
x86-mingw32
DEPENDENCIES
@@ -155,6 +164,7 @@ DEPENDENCIES
rails (= 3.0.5)
rake
redis
redis-namespace
ruby_parser
sass
sqlite3
+4 -1
View File
@@ -1 +1,4 @@
APP_CONFIG = HashWithIndifferentAccess.new(YAML.load(File.open(File.join(Rails.root, 'config', 'config.yml'))))
APP_CONFIG = HashWithIndifferentAccess.new(YAML.load(File.open(File.join(Rails.root, 'config', 'config.yml'))))
require "sevendigital"
$sevendigital_client = Sevendigital::Client.new("config/sevendigital.yml", :country => "US")
+8
View File
@@ -0,0 +1,8 @@
$redis = Redis.new(:host => 'localhost', :port => 6379)
begin
$redis.ping
puts "=> Established connection to redis server"
rescue
puts "=> CAUTION: cound not establish connection to redis server! Check settings in initializer!"
end
+1 -1
View File
@@ -1 +1 @@
require "sevendigital"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+9 -11
View File
@@ -1,16 +1,19 @@
Nineminutes::Application.routes.draw do
get "sidebar/index"
get "charts/index"
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
devise_for :users,
:controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :registrations => "registrations", :sessions => "sessions" }
devise_scope :user do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
get "/users/auth/:provider" => "users/omniauth_callbacks#passthru"
get "/register" => "devise_auth/registrations#new", :as => :new_user_registration
post "/users" => "devise_auth/registrations#create", :as => :user_registration
get "/login" => "devise_auth/sessions#new", :as => :new_user_session
post "/login" => "devise_auth/sessions#create", :as => :user_session
get "/logout" => "devise_auth/sessions#destroy", :as => :destroy_user_session
end
get "tracks/index"
get "artists/get_artists", :as => "get_artists"
match "/artists/:artist", :to => "artists#show"
match "/more_tracks", :to => "artists#more_tracks"
match "/more_albums", :to => "artists#more_albums"
@@ -23,16 +26,11 @@ Nineminutes::Application.routes.draw do
match "/search_video", :to => "tracks#search_video"
#resources :artists, :constraints => { :id => /.*/ }
resources :users
resources :sidebar
resources :search
resources :users, :only => [ :show ]
get "home/", :to => "home#show"
get "charts/", :to => "charts#index"
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => "home#index"
end
+9 -1
View File
@@ -1,6 +1,7 @@
module LastFM
class Album < Request
def self.search query, limit = nil, page = nil
album_request "search", "albummatches", query, nil, limit, page do |result|
result = LastFM::Validator.validate_mash result
@@ -11,7 +12,14 @@ module LastFM
# return: album name, artist, (top)tags, tracks ( name, duration (in sec) ) mbid (album), release date, image urls ( different sizes )
def self.getInfo album, artist
album_request "getInfo", "album", album, artist
RedisQueryable.look_up_key(:artist, { :hashable => [album, artist] }) do
album_request "getInfo", "album", album, artist do |info|
info = LastFM::Validator.validate_mash info, "tracks"
info = Hashie::Mash.new({ :tracks => [] }) if info.blank?
update_results [current_class_name, __method__], info
RedisQueryable.store :artist, { :hashable => [album, artist] }, info.to_json
end
end
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
+31 -13
View File
@@ -12,33 +12,51 @@ module LastFM
# return: artist name, tags, similar artists, artist bio (summary / long), mbid, image urls ( different sizes )
def self.getInfo artist
artist_request "getInfo", "artist", artist do |info|
info = LastFM::Validator.validate_mash info, "tag", "image"
update_results [current_class_name, __method__], info
RedisQueryable.look_up_key(:artist, { :hashable => artist, :type => :info }) do
artist_request "getInfo", "artist", artist do |info|
info = LastFM::Validator.validate_mash info, "tag", "image"
update_results [current_class_name, __method__], info
RedisQueryable.store :artist, { :hashable => artist, :type => :info }, info.to_json
end
end
end
# return: artists ( name, mbid, image urls ( different sizes ) )
def self.getSimilar artist, limit = nil, page = nil
artist_request "getSimilar", "similarartists", artist, limit
RedisQueryable.look_up_key(:artist, { :hashable => artist, :type => :similar }) do
artist_request "getSimilar", "similarartists", artist, limit do |similar|
similar = LastFM::Validator.validate_mash similar, "tag", "image"
update_results [current_class_name, __method__], similar
RedisQueryable.store :artist, { :hashable => artist, :type => :similar }, similar.to_json
end
end
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags artist
artist_request "getTopTags", "toptags", artist
end
# return: tracks ( name, image urls ( different sizes ) ) ordered by popularity
def self.getTopTracks artist, limit = nil, page = nil
artist_request "getTopTracks", "toptracks", artist, limit, page
RedisQueryable.look_up_key(:artist, { :hashable => artist, :type => :tracks, :page => page }) do
artist_request "getTopTracks", "toptracks", artist, limit, page do |tracks|
tracks = LastFM::Validator.validate_mash tracks, "tag", "image"
update_results [current_class_name, __method__], tracks
RedisQueryable.store :artist, { :hashable => artist, :type => :tracks, :page => page }, tracks.to_json
end
end
end
# return: albums ( name, mbid, album url, image urls ( different sizes ) ) ordered by popularity
def self.getTopAlbums artist, limit = nil, page = nil
artist_request "getTopAlbums", "topalbums", artist, limit, page do |albums|
albums = LastFM::Validator.validate_mash albums, "album"
albums = Hashie::Mash.new({ :album => [] }) if albums.total == "0"
update_results [current_class_name, __method__], albums
RedisQueryable.look_up_key(:artist, { :hashable => artist, :type => :albums, :page => page }) do
artist_request "getTopAlbums", "topalbums", artist, limit, page do |albums|
albums = LastFM::Validator.validate_mash albums, "album"
albums = Hashie::Mash.new({ :album => [] }) if albums.total == "0"
update_results [current_class_name, __method__], albums
RedisQueryable.store :artist, { :hashable => artist, :type => :albums, :page => page }, albums.to_json
end
end
end
+13 -6
View File
@@ -2,18 +2,25 @@ module LastFM
class Chart < Request
def self.getTopArtists limit = nil, page = nil
chart_request "getTopArtists", "artists", limit, page do |artists|
LastFM::Validator.validate_mash artists, "image"
update_results [current_class_name, __method__], artists
RedisQueryable.look_up_key(:chart, { :type => :artists, :page => page }) do
chart_request "getTopArtists", "artists", limit, page do |artists|
artists = LastFM::Validator.validate_mash artists, "image"
update_results [current_class_name, __method__], artists
RedisQueryable.store :chart, { :type => :artists, :page => page }, artists.to_json
end
end
end
def self.getTopTracks limit = nil, page = nil
chart_request "getTopTracks", "tracks", limit, page do |tracks|
LastFM::Validator.validate_mash tracks
update_results [current_class_name, __method__], tracks
RedisQueryable.look_up_key(:chart, { :type => :tracks, :page => page }) do
chart_request "getTopTracks", "tracks", limit, page do |tracks|
tracks = LastFM::Validator.validate_mash tracks
update_results [current_class_name, __method__], tracks
RedisQueryable.store :chart, { :type => :tracks, :page => page }, tracks.to_json
end
end
end
end
end
+53
View File
@@ -0,0 +1,53 @@
module RedisQueryable
VALID_NAMESPACES = [ :artist, :album, :chart ]
EXPIRATION = {
:artist => { :tracks => 3, :albums => 7, :similar => 14, :info => 7 },
:chart => { :tracks => 10, :artists => 10 },
:album => 28
}
def self.store namespace, key_hash, value
key = build_key key_hash.values
redis = switch_to_namespace namespace
expires = key_hash.has_key?(:type) ? EXPIRATION[namespace][key_hash[:type]] : EXPIRATION[namespace]
redis.setex key, expires.to_i.days, value
end
def self.get namespace, key_hash
key = build_key key_hash.values
redis = switch_to_namespace namespace
json = redis.get key
Hashie::Mash.new(JSON.parse(json))
end
def self.look_up_key namespace, key_hash, &block
if exists_key? namespace, key_hash
get namespace, key_hash
else
block.call
end
end
private
def self.exists_key? namespace, key_hash
key = build_key key_hash.values
redis = switch_to_namespace namespace
redis.exists key
end
def self.build_key key_array
key_array.flatten.map do |value|
value.is_a?(String) ? Digest::SHA1.hexdigest(value) : value.to_s
end.join(":")
end
def self.switch_to_namespace namespace
return Redis::Namespace.new(namespace, :redis => $redis) if VALID_NAMESPACES.include? namespace
$redis
end
end
+24
View File
@@ -23,4 +23,28 @@ $(document).ready(function(){
show_flash(false);
load_site_request(search_path, this.params['q']);
});
// Sign Up Page
Path.map(hashbang+register_path).to(function(){
show_flash(false);
load_site_request(register_path, "");
});
// Sign In Page
Path.map(hashbang+login_path).to(function(){
show_flash(false);
load_site_request(login_path, "");
});
// Sign Out Page
Path.map(hashbang+logout_path).to(function(){
show_flash(false);
load_site_request(logout_path, "");
});
// User Show Page
Path.map(hashbang+user_path+":user").to(function(){
show_flash(false);
load_site_request(user_path, this.params['user']);
});
});
+4
View File
@@ -7,6 +7,10 @@ const home_path = "/home";
const charts_path = "/charts";
const search_path = "/search/";
const search_autocomplete_path = "/autocomplete/";
const user_path = "/users/";
const register_path = "/register";
const login_path = "/login";
const logout_path = "/logout";
$(document).ready(function(){
Path.root(hashbang+home_path);
+2 -1
View File
@@ -152,7 +152,8 @@ function init_site(link){
init_autocomplete();
set_active_navigation(charts_path);
break;
case user_path:
init_tabs();
}
}
File diff suppressed because one or more lines are too long