forked from hachi/MogileFS-Server
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache abstraction layer with Redis support #19
Open
gigablah
wants to merge
2
commits into
mogilefs:master
Choose a base branch
from
gigablah:cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+312
−65
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package MogileFS::Cache; | ||
use strict; | ||
use MogileFS::Util qw(error); | ||
|
||
my $have_memc_module = eval "use Cache::Memcached; 1;"; | ||
my $have_redis_module = eval "use Redis; 1;"; | ||
|
||
sub new { | ||
my ($class) = @_; | ||
return $class->new_from_config; | ||
} | ||
|
||
sub new_from_config { | ||
my ($class) = @_; | ||
return undef unless ($have_memc_module || $have_redis_module); | ||
|
||
my ($type, $servers, $cache_ttl); | ||
|
||
if ($servers = MogileFS::Config->server_setting_cached('memcache_servers')) { | ||
$type = 'memcache'; | ||
$cache_ttl = MogileFS::Config->server_setting_cached('memcache_ttl') || 3600; | ||
} else { | ||
$servers = MogileFS->config('cache_servers'); | ||
$type = MogileFS->config('cache_type'); | ||
$cache_ttl = MogileFS->config('cache_ttl') || 3600; | ||
} | ||
|
||
my $subclass; | ||
if ($type eq 'none' || $type eq '') { | ||
return undef; | ||
} elsif ($type eq 'memcache' && $have_memc_module) { | ||
$subclass = 'MogileFS::Cache::Memcache'; | ||
} elsif ($type eq 'redis' && $have_redis_module) { | ||
$subclass = 'MogileFS::Cache::Redis'; | ||
} else { | ||
error("Cache type not supported: $type"); | ||
return undef; | ||
} | ||
unless (eval "use $subclass; 1") { | ||
error("Error loading $subclass: $@"); | ||
return undef; | ||
} | ||
|
||
my $self = bless { | ||
servers => $servers, | ||
ttl => $cache_ttl, | ||
}, $subclass; | ||
$self->init; | ||
return $self; | ||
} | ||
|
||
sub init { 1 } | ||
|
||
sub refresh { | ||
my $self = shift; | ||
return $self; | ||
} | ||
|
||
sub set { | ||
my ($self, $key, $value) = @_; | ||
die "set not implemented for $self"; | ||
} | ||
|
||
sub get { | ||
my ($self, $key) = @_; | ||
die "get not implemented for $self"; | ||
} | ||
|
||
sub delete { | ||
my ($self, $key) = @_; | ||
die "delete not implemented for $self"; | ||
} | ||
|
||
sub hash { | ||
my ($self, $key) = @_; | ||
if ($key->{type} eq 'fid') { | ||
return 'mogf:' . $key->{domain} . ':' . $key->{key}; | ||
} elsif ($key->{type} eq 'devid') { | ||
return 'mogd:' . $key->{fid}; | ||
} | ||
return $key; | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
|
||
=head1 NAME | ||
|
||
MogileFS::Cache - path cache provider. base class. | ||
|
||
=head1 ABOUT | ||
|
||
Caches key-path mappings for faster lookup. | ||
|
||
=head1 SEE ALSO | ||
|
||
L<MogileFS::Cache::Memcache> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package MogileFS::Cache::Memcache; | ||
use strict; | ||
use base 'MogileFS::Cache'; | ||
use Cache::Memcached; | ||
|
||
# -------------------------------------------------------------------------- | ||
# Memcache mappings are as follows: | ||
# mogf:<dmid>:<dkey> -> fidid | ||
# mogd:<fidid> -> \@devids (and TODO: invalidate when deletion is run!) | ||
# -------------------------------------------------------------------------- | ||
|
||
my $cache; | ||
|
||
sub init { | ||
my $self = shift; | ||
$self->SUPER::init; | ||
|
||
my @servers = grep(!/^$/, grep(s/^\s*|\s*$//g, split(',', $self->{servers}))); | ||
@servers = ('127.0.0.1:11211') unless @servers; | ||
|
||
$cache = Cache::Memcached->new; | ||
$cache->set_servers(\@servers); | ||
return $self; | ||
} | ||
|
||
sub refresh { | ||
my $self = shift; | ||
|
||
# backwards compatibility with previous cache implementation | ||
my @servers = grep(!/^$/, grep(s/^\s*|\s*$//g, split(',', MogileFS::Config->server_setting_cached("memcache_servers")))); | ||
return undef unless @servers; | ||
|
||
$cache->set_servers(\@servers); | ||
return $self; | ||
} | ||
|
||
sub set { | ||
my ($self, $key, $value) = @_; | ||
return $cache->set($self->hash($key), $value, $self->{ttl}); | ||
} | ||
|
||
sub get { | ||
my ($self, $key) = @_; | ||
return $cache->get($self->hash($key)); | ||
} | ||
|
||
sub delete { | ||
my ($self, $key) = @_; | ||
return $cache->delete($self->hash($key)); | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
|
||
=head1 NAME | ||
|
||
MogileFS::Cache::Memcache - Memcache path cache for MogileFS | ||
|
||
=head1 SEE ALSO | ||
|
||
L<MogileFS::Cache> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package MogileFS::Cache::Redis; | ||
use strict; | ||
use base 'MogileFS::Cache'; | ||
use Redis; | ||
|
||
# -------------------------------------------------------------------------- | ||
# Redis mappings: | ||
# mogf:<dmid>:<dkey> -> fidid | ||
# mogd:<fidid> -> \@devids (Redis set) | ||
# -------------------------------------------------------------------------- | ||
|
||
my $cache; | ||
|
||
sub init { | ||
my $self = shift; | ||
$self->SUPER::init; | ||
|
||
my $server = $self->{servers}; | ||
$server =~ s/^\s+//; | ||
$server =~ s/\s+$//; | ||
$server ||= '127.0.0.1:6379'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling the option "servers" but not handling multiple servers specified seems wrong. :-/ At least read/parse them and warn that only one is supported for Redis, I think. |
||
|
||
if ($server =~ m!^/!) { | ||
$cache = Redis->new(sock => $server, encoding => undef, reconnect => 60); | ||
} else { | ||
$cache = Redis->new(server => $server, encoding => undef, reconnect => 60); | ||
} | ||
return $self; | ||
} | ||
|
||
sub refresh { | ||
my $self = shift; | ||
return $self; | ||
} | ||
|
||
sub set { | ||
# We use simple keys and sets since Redis only supports | ||
# expiry based on key rather than individual elements of | ||
# an advanced data structure like hashes. | ||
my ($self, $key, $value) = @_; | ||
my $hash = $self->hash($key); | ||
my $response; | ||
if ($key->{type} eq 'devid') { | ||
# requires Redis >= 2.4 | ||
$response = $cache->sadd($hash, @$value); | ||
} else { | ||
$response = $cache->set($hash => $value); | ||
} | ||
# Unlike memcache, setting ttl to 0 will clear the key | ||
if ($self->{ttl}) { | ||
$cache->expire($hash, $self->{ttl}); | ||
} | ||
return $response; | ||
} | ||
|
||
sub get { | ||
my ($self, $key) = @_; | ||
my $hash = $self->hash($key); | ||
if ($key->{type} eq 'devid') { | ||
my $members = $cache->smembers($hash); | ||
return @$members ? $members : undef; | ||
} | ||
return $cache->get($hash); | ||
} | ||
|
||
sub delete { | ||
my ($self, $key) = @_; | ||
return $cache->del($self->hash($key)); | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
|
||
=head1 NAME | ||
|
||
MogileFS::Cache::Redis - Redis path cache for MogileFS | ||
|
||
=head1 SEE ALSO | ||
|
||
L<MogileFS::Cache> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like it'd be more helpful to either default to something here or print a warning that cache_type must be specified with cache_servers.