| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # NAME: Redis CappedCollection demonstration | ||
| 4 | |||
| 5 | use 5.010; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | #-- Common --------------------------------------------------------------------- | ||
| 10 | use Redis::CappedCollection qw( | ||
| 11 | $DEFAULT_SERVER | ||
| 12 | $DEFAULT_PORT | ||
| 13 | |||
| 14 | $E_NO_ERROR | ||
| 15 | $E_MISMATCH_ARG | ||
| 16 | $E_DATA_TOO_LARGE | ||
| 17 | $E_NETWORK | ||
| 18 | $E_MAXMEMORY_LIMIT | ||
| 19 | $E_MAXMEMORY_POLICY | ||
| 20 | $E_COLLECTION_DELETED | ||
| 21 | $E_REDIS | ||
| 22 | $E_DATA_ID_EXISTS | ||
| 23 | $E_OLDER_THAN_ALLOWED | ||
| 24 | ); | ||
| 25 | |||
| 26 | my $server = $DEFAULT_SERVER.":".$DEFAULT_PORT; # the Redis Server | ||
| 27 | |||
| 28 | sub exception { | ||
| 29 | my $coll = shift; | ||
| 30 | my $err = shift; | ||
| 31 | |||
| 32 | die $err unless $coll; | ||
| 33 | if ( $coll->last_errorcode == $E_NO_ERROR ) | ||
| 34 | { | ||
| 35 | # For example, to ignore | ||
| 36 | return unless $err; | ||
| 37 | } | ||
| 38 | elsif ( $coll->last_errorcode == $E_MISMATCH_ARG ) | ||
| 39 | { | ||
| 40 | # Necessary to correct the code | ||
| 41 | } | ||
| 42 | elsif ( $coll->last_errorcode == $E_DATA_TOO_LARGE ) | ||
| 43 | { | ||
| 44 | # You must use the control data length | ||
| 45 | } | ||
| 46 | elsif ( $coll->last_errorcode == $E_NETWORK ) | ||
| 47 | { | ||
| 48 | # For example, sleep | ||
| 49 | #sleep 60; | ||
| 50 | # and return code to repeat the operation | ||
| 51 | #return 'to repeat'; | ||
| 52 | } | ||
| 53 | elsif ( $coll->last_errorcode == $E_MAXMEMORY_LIMIT ) | ||
| 54 | { | ||
| 55 | # For example, return code to restart the server | ||
| 56 | #return 'to restart the redis server'; | ||
| 57 | } | ||
| 58 | elsif ( $coll->last_errorcode == $E_MAXMEMORY_POLICY ) | ||
| 59 | { | ||
| 60 | # Correct Redis server 'maxmemory-policy' setting | ||
| 61 | } | ||
| 62 | elsif ( $coll->last_errorcode == $E_COLLECTION_DELETED ) | ||
| 63 | { | ||
| 64 | # For example, return code to ignore | ||
| 65 | #return "to ignore $err"; | ||
| 66 | } | ||
| 67 | elsif ( $coll->last_errorcode == $E_REDIS ) | ||
| 68 | { | ||
| 69 | # Independently analyze the $err | ||
| 70 | } | ||
| 71 | elsif ( $coll->last_errorcode == $E_DATA_ID_EXISTS ) | ||
| 72 | { | ||
| 73 | # For example, return code to reinsert the data | ||
| 74 | #return "to reinsert with new data ID"; | ||
| 75 | } | ||
| 76 | elsif ( $coll->last_errorcode == $E_OLDER_THAN_ALLOWED ) | ||
| 77 | { | ||
| 78 | # Independently analyze the situation | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | # Unknown error code | ||
| 83 | } | ||
| 84 | die $err if $err; | ||
| 85 | } | ||
| 86 | |||
| 87 | my ( $id, $coll, @data ); | ||
| 88 | |||
| 89 | eval { | ||
| 90 | $coll = Redis::CappedCollection->create( | ||
| 91 | redis => $server, | ||
| 92 | name => 'Some name', # Create a collection with the specified name | ||
| 93 | ); | ||
| 94 | }; | ||
| 95 | exception( $coll, $@ ) if $@; | ||
| 96 | print "'", $coll->name, "' collection created.\n"; | ||
| 97 | |||
| 98 | #-- Producer ------------------------------------------------------------------- | ||
| 99 | #-- New data | ||
| 100 | |||
| 101 | eval { | ||
| 102 | $id = $coll->insert( | ||
| 103 | 'Some_unique_id', | ||
| 104 | 123, # data id | ||
| 105 | 'Some data stuff', | ||
| 106 | ); | ||
| 107 | print "Added data in a list with '", $id, "' id\n"; | ||
| 108 | |||
| 109 | if ( $coll->update( $id, 0, 'Some new data stuff' ) ) | ||
| 110 | { | ||
| 111 | print "Data updated successfully\n"; | ||
| 112 | } | ||
| 113 | else | ||
| 114 | { | ||
| 115 | print "The data is not updated\n"; | ||
| 116 | } | ||
| 117 | }; | ||
| 118 | exception( $coll, $@ ) if $@; | ||
| 119 | |||
| 120 | #-- Consumer ------------------------------------------------------------------- | ||
| 121 | #-- Fetching the data | ||
| 122 | |||
| 123 | eval { | ||
| 124 | @data = $coll->receive( $id ); | ||
| 125 | print "List '$id' has '$_'\n" foreach @data; | ||
| 126 | |||
| 127 | # or | ||
| 128 | while ( my ( $id, $data ) = $coll->pop_oldest ) | ||
| 129 | { | ||
| 130 | print "List '$id' had '$data'\n"; | ||
| 131 | } | ||
| 132 | }; | ||
| 133 | exception( $coll, $@ ) if $@; | ||
| 134 | |||
| 135 | #-- Utility -------------------------------------------------------------------- | ||
| 136 | #-- Getting statistics | ||
| 137 | |||
| 138 | my ( $lists, $items ); | ||
| 139 | eval { | ||
| 140 | my $info = $coll->collection_info; | ||
| 141 | print 'An existing collection uses: ', | ||
| 142 | $info->{items}, ' items are placed in ', | ||
| 143 | $info->{lists}, ' lists', "\n"; | ||
| 144 | |||
| 145 | print "The collection has '$id' list\n" if $coll->list_exists( 'Some_id' ); | ||
| 146 | |||
| 147 | print "Collection '", $coll->name, "' has '$_' list\n" foreach $coll->lists; | ||
| 148 | }; | ||
| 149 | exception( $coll, $@ ) if $@; | ||
| 150 | |||
| 151 | #-- Closes and cleans up ------------------------------------------------------- | ||
| 152 | |||
| 153 | eval { | ||
| 154 | $coll->quit; | ||
| 155 | |||
| 156 | # Before use, make sure that the collection is not being used by other customers | ||
| 157 | # $coll->drop_collection; | ||
| 158 | }; | ||
| 159 | exception( $coll, $@ ) if $@; | ||
| 160 | |||
| 161 | exit; |