summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Woodward2006-04-11 05:57:46 +0000
committerJason Woodward2006-04-11 05:57:46 +0000
commit942add889321c982b3354f628da52d0b931220b9 (patch)
tree463e9a0f754b5af077d020b07fd6699afea6f6a7
parent94d4d84ee00bfbae01056d7fb25f9a6193b87311 (diff)
downloadJaos-DBI-942add889321c982b3354f628da52d0b931220b9.tar.gz
added cache object expiration
-rw-r--r--lib/Jaos/DBI.pm33
-rw-r--r--t/01use.t4
-rw-r--r--t/04full.t4
-rw-r--r--t/cache.t6
4 files changed, 39 insertions, 8 deletions
diff --git a/lib/Jaos/DBI.pm b/lib/Jaos/DBI.pm
index d74b462..7f6aa48 100644
--- a/lib/Jaos/DBI.pm
+++ b/lib/Jaos/DBI.pm
@@ -34,8 +34,11 @@ Jaos::DBI - Jaos DBI object similar to Class::DBI
__PACKAGE__->sequence('users_id_seq');
__PACKAGE__->base_select('select a.*,b.join_id,b.join_name from foo a join bar b on a.id=b.foo');
__PACKAGE__->has_a(role => 'Role');
- __PACKAGE__->manual_update(1);
- __PACKAGE__->cache(1);
+ __PACKAGE__->manual_update(1); # 0/1
+ __PACKAGE__->cache(1); # 0/1
+ __PACKAGE__->cache_servers( qw/ 127.0.0.1:11211 10.1.1.2:11212 / );
+ __PACKAGE__->compress_threshold( 10_000 );
+ __PACKAGE__->cache_expires(60); # in seconds
__PACKAGE__->setup(); # initiates database connection callback and accessors
1;
@@ -84,6 +87,8 @@ The aim is to keep it simple and avoid the db statement hit from Class::DBI
and make it easier to use views, complex sql joins for basic class definitions,
and get rid of the large requirements for external modules used by Class::DBI.
+Cache::Memcached is used optionally for network available object caching.
+
=cut
__PACKAGE__->mk_classdata('_table');
@@ -97,12 +102,13 @@ __PACKAGE__->mk_classdata('_db_user');
__PACKAGE__->mk_classdata('_db_password');
__PACKAGE__->mk_classdata('_db_options');
__PACKAGE__->mk_classdata('_base_select');
-__PACKAGE__->mk_classdata('_manual_update' => 0);
-__PACKAGE__->mk_classdata('_is_changed' => 0);
+__PACKAGE__->mk_classdata('_manual_update' => 0); # 0/1
+__PACKAGE__->mk_classdata('_is_changed' => 0); # 0/1
__PACKAGE__->mk_classdata('_has_a');
__PACKAGE__->mk_classdata('_cache');
__PACKAGE__->mk_classdata('_cache_servers' => ['127.0.0.1:11211']);
__PACKAGE__->mk_classdata('_compress_threshold' => 10_000);
+__PACKAGE__->mk_classdata('_cache_expires' => 60); # in seconds
=head1 METHODS
@@ -965,6 +971,23 @@ sub compress_threshold
$self->_compress_threshold();
}
+=head2 cache_expires
+
+Sets the expiration time on the persistant cached objects in seconds.
+
+ __PACKAGE__->cache_expires(60); # in seconds
+
+=cut
+
+sub cache_expires
+{
+ my $self = shift;
+ if (@_) {
+ $self->_cache_expires(@_);
+ }
+ $self->_cache_expires();
+}
+
sub _add_obj_to_cache
{
my ($self,$obj) = @_;
@@ -974,7 +997,7 @@ sub _add_obj_to_cache
if (my $key = $obj->primary_column) {
if (my $id = $obj->{$key}) {
- $Jaos::DBI::cache->set("$class:$id",$obj);
+ $Jaos::DBI::cache->set("$class:$id",$obj,$self->_cache_expires);
}
}
diff --git a/t/01use.t b/t/01use.t
index fd96675..7f54961 100644
--- a/t/01use.t
+++ b/t/01use.t
@@ -3,12 +3,14 @@ use Test::More tests => 2;
use_ok('Jaos::DBI');
my @base_class_methods = qw/
db_dsn db_user db_password db_options
- setup base_select
+ setup base_select is_changed has_a
table columns primary_column virtual_columns manual_update
sequence sequence_nextval sequence_currval
insert create find_or_create delete set get update
search search_like search_ilike search_or search_or_like search_or_ilike
search_where retrieve_all retrieve
prepare begin_work commit rollback
+ cache cache_servers compress_threshold cache_expires
+
/;
can_ok('Jaos::DBI',@base_class_methods);
diff --git a/t/04full.t b/t/04full.t
index 5a376b0..7e53d4e 100644
--- a/t/04full.t
+++ b/t/04full.t
@@ -26,13 +26,15 @@ BEGIN
my @base_class_methods = qw/
db_dsn db_user db_password db_options
- setup base_select is_changed has_a cache
+ setup base_select is_changed has_a
table columns primary_column virtual_columns manual_update
sequence sequence_nextval sequence_currval
insert create find_or_create delete set get update
search search_like search_ilike search_or search_or_like search_or_ilike
search_where retrieve_all retrieve
prepare begin_work commit rollback
+ cache cache_servers compress_threshold cache_expires
+
/;
can_ok('Foo',@base_class_methods);
diff --git a/t/cache.t b/t/cache.t
index 9e7b80d..6c44a15 100644
--- a/t/cache.t
+++ b/t/cache.t
@@ -18,7 +18,7 @@ BEGIN
or plan skip_all =>
"Cache::Memcached is needed for this test";
- plan tests => 7;
+ plan tests => 10;
unlink('baz_table') if ( -e 'baz_table' );
unlink('bar_table') if ( -e 'bar_table' );
@@ -40,6 +40,10 @@ BEGIN
}
ok(Bar->cache(1),'setting cache');
+ok(Bar->cache_expires(60),'setting cache expiration');
+ok(Bar->cache_servers(qw/127.0.0.1:11211/),'setting cache servers');
+ok(Bar->compress_threshold(10_000),'setting cache servers');
+
ok(4 == Bar->retrieve_all(),'retrieve all objects');
ok(4 == scalar keys %{$Jaos::DBI::cache->get_multi( qw/Bar:1 Bar:2 Bar:3 Bar:4/ )},'check cache is initially populated');