Debian Codegraph @

cmmtest

raw
1 #!/usr/local/bin/perl-journals
2
3 # cmmtest: test of mmapped cache
4 # $Id: cmmtest,v 1.5 2003/12/23 11:46:31 pmh Exp $
5
6 use Cache::Mmap;
7 use Data::Dumper;
8 use strict;
9
10 # These will be read from the file if it already exists
11 my %options=(
12 buckets => 11,
13 bucketsize => 200,
14 strings => 1,
15 );
16
17 my %escape=(
18 "\n" => '\n',
19 "\r" => '\r',
20 "\t" => '\t',
21 "\\" => '\\\\',
22 );
23
24
25 my $filename='.cache';
26 $filename=shift if @ARGV;
27 my $cache=Cache::Mmap->new($filename,\%options)
28 or die;
29
30 print "Strings: ",$cache->strings,"\n";
31 # Read user commands from STDIN
32 $|=1;
33 print "cmmtest: ";
34 while(<>){
35 my($cmd,$key,$value)=/^([<>0?\-k])(?:\s+(\S+)(?:\s+(.*))?)?\s+$/
36 or do{ warn "Invalid command\n"; next; };
37
38 if($cmd eq '<'){
39 # Read
40 if($value ne ''){
41 warn "Unexpected stuff after key\n";
42 next;
43 }
44 my($found,$val)=$cache->read($key);
45 if(!$found){
46 print "No such value\n";
47 }elsif(defined $val){
48 if($cache->{strings}){
49 $val=~s/([^\40-\176\\])/$escape{$1} || sprintf '\\x{%02x}',ord $1/ge;
50 }else{
51 $val=Dumper($val);
52 }
53 print "=> $val\n";
54 }else{
55 print "Undefined value\n";
56 }
57 }elsif($cmd eq '>'){
58 # Write
59 if($cache->write($key,$cache->strings ? $value : \$value)){
60 print "OK\n";
61 }else{
62 warn "Can't write value\n";
63 }
64 }elsif($cmd eq '-'){
65 # delete
66 my($found,$val)=$cache->delete($key);
67 if(!$found){
68 print "No such value\n";
69 }elsif(defined $val){
70 $val=Dumper($val) unless $cache->strings;
71 print "= ok[$val]\n";
72 }else{
73 warn "Undefined value\n";
74 }
75 }elsif($cmd eq '0'){
76 # clear
77 $cache->quick_clear();
78 print "OK\n";
79 }elsif($cmd eq 'k'){
80 print map " $_\n",$cache->entries;
81 print "OK\n";
82 }elsif($cmd eq '?'){
83 # exists
84 my($found,$val)=$cache->read($key);
85 if($found){
86 print "= yes\n";
87 }else{
88 print "= no\n";
89 }
90 }else{
91 warn "Unknown command: $cmd\n";
92 }
93 }continue{
94 print "cmmtest: ";
95 }
96