| 1 | package OpenInteract::Cache; | ||
| 2 | |||
| 3 | # $Id: Cache.pm,v 1.6 2003/04/07 02:55:57 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | # Returns: caching object (implementation-neutral) | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ( $pkg, @params ) = @_; | ||
| 11 | my $class = ref $pkg || $pkg; | ||
| 12 | my $self = bless( {}, $class ); | ||
| 13 | $self->{_cache_object} = $self->initialize( @params ); | ||
| 14 | return $self; | ||
| 15 | } | ||
| 16 | |||
| 17 | |||
| 18 | # Returns: data from the cache | ||
| 19 | |||
| 20 | sub get { | ||
| 21 | my ( $self, $p ) = @_; | ||
| 22 | |||
| 23 | # if the cache hasn't been initialized, bail | ||
| 24 | return undef unless ( $self->{_cache_object} ); | ||
| 25 | |||
| 26 | my $key = $p->{key}; | ||
| 27 | my $is_object = 0; | ||
| 28 | my $obj_class = undef; | ||
| 29 | my $R = OpenInteract::Request->instance; | ||
| 30 | if ( ! $key and $p->{class} and $p->{object_id} ) { | ||
| 31 | $key = _make_spops_idx( $p->{class}, $p->{object_id} ); | ||
| 32 | $R->DEBUG && $R->scrib( 3, "Created class+id key [$key]" ); | ||
| 33 | $obj_class = $p->{class}; | ||
| 34 | $is_object++; | ||
| 35 | return undef unless ( $obj_class->pre_cache_get( $p->{object_id} ) ); | ||
| 36 | } | ||
| 37 | unless ( $key ) { | ||
| 38 | $R->DEBUG && $R->scrib( 2, "Cache MISS (no key)" ); | ||
| 39 | return undef; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $data = $self->get_data( $self->{_cache_object}, $key ); | ||
| 43 | unless ( $data ) { | ||
| 44 | $R->DEBUG && $R->scrib( 2, "Cache MISS [$key]" ); | ||
| 45 | return undef; | ||
| 46 | } | ||
| 47 | |||
| 48 | $R->DEBUG && $R->scrib( 2, "Cache HIT [$key]" ); | ||
| 49 | if ( $is_object ) { | ||
| 50 | return undef unless ( $obj_class->post_cache_get( $data ) ); | ||
| 51 | } | ||
| 52 | return $data; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub set { | ||
| 56 | my ( $self, $p ) = @_; | ||
| 57 | |||
| 58 | # if the cache hasn't been initialized, bail | ||
| 59 | return undef unless ( $self->{_cache_object} ); | ||
| 60 | |||
| 61 | my $is_object = 0; | ||
| 62 | my $key = $p->{key}; | ||
| 63 | my $data = $p->{data}; | ||
| 64 | my ( $obj ); | ||
| 65 | my $R = OpenInteract::Request->instance; | ||
| 66 | if ( _is_object( $data ) ) { | ||
| 67 | $obj = $data; | ||
| 68 | $key = _make_spops_idx( ref $obj, scalar( $obj->id ) ); | ||
| 69 | $R->DEBUG && $R->scrib( 2, "Created class/id key [$key]" ); | ||
| 70 | $is_object++; | ||
| 71 | return undef unless ( $obj->pre_cache_save ); | ||
| 72 | $data = $obj->as_data_only; | ||
| 73 | } | ||
| 74 | $self->set_data( $self->{_cache_object}, $key, $data, $p->{expire} ); | ||
| 75 | if ( $obj and $obj->can( 'post_cache_save' ) ) { | ||
| 76 | return undef if ( $obj->post_cache_save ); | ||
| 77 | } | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub clear { | ||
| 82 | my ( $self, $p ) = @_; | ||
| 83 | |||
| 84 | # if the cache hasn't been initialized, bail | ||
| 85 | return undef unless ( $self->{_cache_object} ); | ||
| 86 | |||
| 87 | my $key = $p->{key}; | ||
| 88 | if ( ! $key and _is_object( $p->{data} ) ) { | ||
| 89 | $key = _make_spops_idx( ref $p->{data}, scalar( $p->{data}->id ) ); | ||
| 90 | } | ||
| 91 | elsif ( ! $key and $p->{class} and $p->{object_id} ) { | ||
| 92 | $key = _make_spops_idx( $p->{class}, $p->{object_id} ); | ||
| 93 | } | ||
| 94 | my $R = OpenInteract::Request->instance; | ||
| 95 | $R->DEBUG && $R->scrib( 2, "Trying to clear cache of [$key]" ); | ||
| 96 | return $self->clear_data( $self->{_cache_object}, $key ); | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub purge { | ||
| 101 | my ( $self ) = @_; | ||
| 102 | |||
| 103 | # if the cache hasn't been initialized, bail | ||
| 104 | return undef unless ( $self->{_cache_object} ); | ||
| 105 | |||
| 106 | my $R = OpenInteract::Request->instance; | ||
| 107 | $R->DEBUG && $R->scrib( 2, "Trying to purge cache of all objects" ); | ||
| 108 | return $self->purge_all( $self->{_cache_object} ); | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub _is_object { | ||
| 113 | my ( $item ) = @_; | ||
| 114 | my $typeof = ref $item; | ||
| 115 | return undef if ( ! $typeof ); | ||
| 116 | return undef if ( $typeof =~ /^(HASH|ARRAY|SCALAR)$/ ); | ||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _make_spops_idx { | ||
| 121 | return join '--', $_[0], $_[1]; | ||
| 122 | } | ||
| 123 | |||
| 124 | ######################################## | ||
| 125 | # SUBCLASS TO OVERRIDE | ||
| 126 | |||
| 127 | sub initialize { die "Subclass must define initialize()\n" } | ||
| 128 | sub get_data { die "Subclass must define get_data()\n" } | ||
| 129 | sub set_data { die "Subclass must define set_data()\n" } | ||
| 130 | sub clear_data { die "Subclass must define clear_data()\n" } | ||
| 131 | sub purge_all { die "Subclass must define purge_all()\n" } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ |