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
+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