Wednesday, March 19, 2008

memcached for Windows

Tuesday, February 27, 2007

Catalyst::Plugin::Session::Store::Memcached hack

Catalyst::Plugin::Session::Store::Memcached is much faster than DBIC. but that's not so fit our demand.
we have one function called "Ban a user", we remove the session of that user when he is banned. but Memcached Store don't have user_id field as key.

Cache::Memcached::Managed has one function named "group". so I hack that pm to support delete_session_data_by_user_id.

first when new Cache::Memcached::Managed we add:
        my $storage = $cfg->{memcached_obj} || Cache::Memcached::Managed->new(
data => "localhost:11211",
namespace => "catalyst_session",
group_names => [qw(user_id)],
%{ $cfg->{memcached_new_args} || {} },
),
so that we can set user_id when set a memcache key.
    $c->_session_memcached_storage->set(
@{ $c->_session_memcached_arg_fudge },
(
$key =~ /^(?:expires|session|flash)/
? ( expiration => $c->session_expires )
: ()
),
id => $key,
value => $data,
user_id => $user_id,
)
at end, delete_session_data_by_user_id is pretty simple.
sub delete_session_data_by_user_id {
my ( $c, $user_id ) = @_;

return unless ($user_id > 0);
$c->_session_memcached_storage->delete_group( user_id => $user_id );
}

have fun!

Labels: ,

Cache::Memcached::Managed

Cache::Memcached::Managed supports ->delete_group. so if u are not satisfied with Cache::Memcached, u may want to have a look at that. :)

Labels: