[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 package DBI::Util::CacheMemory; 2 3 # $Id: CacheMemory.pm 10314 2007-11-26 22:25:33Z timbo $ 4 # 5 # Copyright (c) 2007, Tim Bunce, Ireland 6 # 7 # You may distribute under the terms of either the GNU General Public 8 # License or the Artistic License, as specified in the Perl README file. 9 10 use strict; 11 use warnings; 12 13 =head1 NAME 14 15 DBI::Util::CacheMemory - a very fast but very minimal subset of Cache::Memory 16 17 =head1 DESCRIPTION 18 19 Like Cache::Memory (part of the Cache distribution) but doesn't support any fancy features. 20 21 This module aims to be a very fast compatible strict sub-set for simple cases, 22 such as basic client-side caching for DBD::Gofer. 23 24 Like Cache::Memory, and other caches in the Cache and Cache::Cache 25 distributions, the data will remain in the cache until cleared, it expires, 26 or the process dies. The cache object simply going out of scope will I<not> 27 destroy the data. 28 29 =head1 METHODS WITH CHANGES 30 31 =head2 new 32 33 All options except C<namespace> are ignored. 34 35 =head2 set 36 37 Doesn't support expiry. 38 39 =head2 purge 40 41 Same as clear() - deletes everything in the namespace. 42 43 =head1 METHODS WITHOUT CHANGES 44 45 =over 46 47 =item clear 48 49 =item count 50 51 =item exists 52 53 =item remove 54 55 =back 56 57 =head1 UNSUPPORTED METHODS 58 59 If it's not listed above, it's not supported. 60 61 =cut 62 63 our $VERSION = sprintf("0.%06d", q$Revision: 10314 $ =~ /(\d+)/o); 64 65 my %cache; 66 67 sub new { 68 my ($class, %options ) = @_; 69 my $namespace = $options{namespace} ||= 'Default'; 70 #$options{_cache} = \%cache; # can be handy for debugging/dumping 71 my $self = bless \%options => $class; 72 $cache{ $namespace } ||= {}; # init - ensure it exists 73 return $self; 74 } 75 76 sub set { 77 my ($self, $key, $value) = @_; 78 $cache{ $self->{namespace} }->{$key} = $value; 79 } 80 81 sub get { 82 my ($self, $key) = @_; 83 return $cache{ $self->{namespace} }->{$key}; 84 } 85 86 sub exists { 87 my ($self, $key) = @_; 88 return exists $cache{ $self->{namespace} }->{$key}; 89 } 90 91 sub remove { 92 my ($self, $key) = @_; 93 return delete $cache{ $self->{namespace} }->{$key}; 94 } 95 96 sub purge { 97 return shift->clear; 98 } 99 100 sub clear { 101 $cache{ shift->{namespace} } = {}; 102 } 103 104 sub count { 105 return scalar keys %{ $cache{ shift->{namespace} } }; 106 } 107 108 sub size { 109 my $c = $cache{ shift->{namespace} }; 110 my $size = 0; 111 while ( my ($k,$v) = each %$c ) { 112 $size += length($k) + length($v); 113 } 114 return $size; 115 } 116 117 1;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 17 22:47:18 2015 | Cross-referenced by PHPXref 0.7.1 |