This file is indexed.

/usr/share/perl5/DBIx/SearchBuilder/Record/Cachable.pm is in libdbix-searchbuilder-perl 1.66-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# $Header: /home/jesse/DBIx-SearchBuilder/history/SearchBuilder/Record/Cachable.pm,v 1.6 2001/06/19 04:22:32 jesse Exp $
# by Matt Knopp <mhat@netlag.com>

package DBIx::SearchBuilder::Record::Cachable;

use strict;
use warnings;

use DBIx::SearchBuilder::Handle;
use Cache::Simple::TimedExpiry;

use base qw(DBIx::SearchBuilder::Record);

=head1 NAME

DBIx::SearchBuilder::Record::Cachable - Records with caching behavior

=head1 SYNOPSIS

  package MyRecord;
  use base qw/DBIx::SearchBuilder::Record::Cachable/;

=head1 DESCRIPTION

This module subclasses the main L<DBIx::SearchBuilder::Record> package
to add a caching layer.

The public interface remains the same, except that records which have
been loaded in the last few seconds may be reused by subsequent fetch
or load methods without retrieving them from the database.

=head1 METHODS

=cut


my %_CACHES = ();

sub _SetupCache {
    my ($self, $cache) = @_;
    $_CACHES{$cache} = Cache::Simple::TimedExpiry->new();
    $_CACHES{$cache}->expire_after( $self->_CacheConfig->{'cache_for_sec'} );
    return $_CACHES{$cache};
}


=head2 FlushCache

This class method flushes the _global_ DBIx::SearchBuilder::Record::Cachable 
cache. All caches are immediately expired.

=cut

sub FlushCache {
    %_CACHES = ();
}

=head2 _FlushKeyCache

Blow away this record type's key cache

=cut

sub _FlushKeyCache {
    my $self = shift;
    my $cache = ($self->{_class}||= ref($self))."-KEYS";
    return $self->_SetupCache($cache);
}

sub _KeyCache {
    my $self = shift;
    my $cache = ($self->{_class}||= ref($self))."-KEYS";
    return $_CACHES{$cache} || $self->_SetupCache($cache);
}

sub _RecordCache {
    my $self = shift;
    my $cache = ($self->{_class}||= ref($self));
    return $_CACHES{$cache} || $self->_SetupCache($cache);
}

# Function: LoadFromHash
# Type    : (overloaded) public instance
# Args    : See DBIx::SearchBuilder::Record::LoadFromHash
# Lvalue  : array(boolean, message)

sub LoadFromHash {
    my $self = shift;

    # Blow away the primary cache key since we're loading.
    $self->{'_SB_Record_Primary_RecordCache_key'} = undef;
    my ( $rvalue, $msg ) = $self->SUPER::LoadFromHash(@_);

    ## Check the return value, if its good, cache it!
    $self->_store if $rvalue;

    return ( $rvalue, $msg );
}

# Function: LoadByCols
# Type    : (overloaded) public instance
# Args    : see DBIx::SearchBuilder::Record::LoadByCols
# Lvalue  : array(boolean, message)

sub LoadByCols {
    my ( $self, %attr ) = @_;

    # Blow away the primary cache key since we're loading.
    $self->{'_SB_Record_Primary_RecordCache_key'} = undef;

    # generate the alternate cache key
    my $alt_key = $self->_gen_alternate_RecordCache_key(%attr);
    # get primary cache key
    my $cache_key = $self->_lookup_primary_RecordCache_key($alt_key);
    if ( $cache_key && $self->_fetch( $cache_key ) ) {
        return ( 1, "Fetched from cache" );
    }

    # Fetch from the DB!
    my ( $rvalue, $msg ) = $self->SUPER::LoadByCols(%attr);
    # Check the return value, if its good, cache it!
    if ($rvalue) {
        $self->_store();
        # store alt_key as alias for pk
        $self->_KeyCache->set( $alt_key, $self->_primary_RecordCache_key);
    }
    return ( $rvalue, $msg );
}

# Function: __Set
# Type    : (overloaded) public instance
# Args    : see DBIx::SearchBuilder::Record::_Set
# Lvalue  : ?

sub __Set () {
    my $self = shift;

    $self->_expire;

    return $self->SUPER::__Set( @_ );
}

# Function: Delete
# Type    : (overloaded) public instance
# Args    : nil
# Lvalue  : ?

sub __Delete () {
    my $self = shift;

    $self->_expire;

    return $self->SUPER::__Delete( @_ );

}

# Function: _expire
# Type    : private instance
# Args    : string(cache_key)
# Lvalue  : 1
# Desc    : Removes this object from the cache.

sub _expire (\$) {
    my $self = shift;
    my $cache_key = $self->_primary_RecordCache_key or return;
    $self->_RecordCache->set( $cache_key, undef, time-1 );
    # We should be doing something more surgical to clean out the
    # key cache. but we do need to expire it
    $self->_FlushKeyCache;
}

# Function: _fetch
# Type    : private instance
# Args    : string(cache_key)
# Lvalue  : 1
# Desc    : Get an object from the cache, and make this object that.

sub _fetch () {
    my ( $self, $cache_key ) = @_;
    my $data = $self->_RecordCache->fetch( $cache_key ) or return 0;
    @{$self}{keys %$data} = values %$data; # deserialize
    return 1;
}


# Function: _store
# Type    : private instance
# Args    : string(cache_key)
# Lvalue  : 1
# Desc    : Stores this object in the cache.

sub _store (\$) {
    my $self = shift;
    my $key = $self->_primary_RecordCache_key or return 0;
    $self->_RecordCache->set( $key, $self->_serialize );
    return 1;
}

sub _serialize {
    my $self = shift;
    return {
        values  => $self->{'values'},
        table   => $self->Table,
        fetched => $self->{'fetched'}
    };
}

# Function: _gen_alternate_RecordCache_key
# Type    : private instance
# Args    : hash (attr)
# Lvalue  : 1
# Desc    : Takes a perl hash and generates a key from it.

sub _gen_alternate_RecordCache_key {
    my ( $self, %attr ) = @_;
    my $cache_key = '';
    foreach my $key ( sort keys %attr ) {
        my $value = $attr{$key};
        unless ( defined $value ) {
            $value = '=__undef';
        }
        elsif ( ref($value) eq "HASH" ) {
            $value = ( $value->{operator} || '=' )
                . ( defined $value->{value}? $value->{value}: '__undef' );
        }
        else {
            $value = "=" . $value;
        }
        $cache_key .= $key . $value . ',';
    }
    chop($cache_key);
    return ($cache_key);
}

# Function: _primary_RecordCache_key
# Type    : private instance
# Args    : none
# Lvalue: : 1
# Desc    : generate a primary-key based variant of this object's cache key
#           primary keys is in the cache

sub _primary_RecordCache_key {
    my ($self) = @_;

    return $self->{'_SB_Record_Primary_RecordCache_key'}
        if $self->{'_SB_Record_Primary_RecordCache_key'};

    my $cache_key = '';
    my %pk = $self->PrimaryKeys;
    foreach my $key ( sort keys %pk ) {
        my $value = $pk{$key};
        return undef unless defined $value;
        $cache_key .= $key . '=' . $value .',';
    }
    chop $cache_key;
    return $self->{'_SB_Record_Primary_RecordCache_key'} = $cache_key;
}

# Function: lookup_primary_RecordCache_key
# Type    : private class
# Args    : string(alternate cache id)
# Lvalue  : string(cache id)

sub _lookup_primary_RecordCache_key {
    my ($self, $key) = @_;
    return undef unless $key;
    return $self->_KeyCache->fetch($key) || $key;
}

=head2 _CacheConfig 

You can override this method to change the duration of the caching from the default of 5 seconds. 

For example, to cache records for up to 30 seconds, add the following method to your class:

  sub _CacheConfig {
      { 'cache_for_sec' => 30 }
  }

=cut

sub _CacheConfig {
    return {
        'cache_for_sec' => 5,
    };
}

1;

__END__


=head1 AUTHOR

Matt Knopp <mhat@netlag.com>

=head1 SEE ALSO

L<DBIx::SearchBuilder>, L<DBIx::SearchBuilder::Record>

=cut