/usr/share/perl5/Cache/BDB.pod is in libcache-bdb-perl 0.04-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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | =head1 NAME
Cache::BDB - An object caching wrapper around BerkeleyDB
=head1 SYNOPSIS
use Cache::BDB;
my %options = (
cache_root => "/tmp/caches",
namespace => "Some::Namespace",
default_expires_in => 300, # seconds
);
my $cache = Cache::BDB->new(%options);
#
# [myshellprompt:~]$ find /tmp/caches
# /tmp/caches/Some::Namespace/
# /tmp/caches/Some::Namespace/Some::Namespace.db
# /tmp/caches/Some::Namespace/__db.001
# /tmp/caches/Some::Namespace/__db.002
# /tmp/caches/Some::Namespace/__db.003
#
$cache->namespace(); # returns "Some::Namespace", read only
$cache->default_expires_in(); # returns 300
$cache->default_expires_in(600); # change it to 600
$cache->set(1, \%some_hash);
$cache->set('foo', 'bar');
$cache->set(20, $obj, 10);
$cache->add(21, 'whatever'); # works, nothing with the key '21' set yet.
$cache->add(21, 'coffeepot'); # fails, can only add() something that hasn't
# yet been set
$cache->replace(21, 'shoelace'); # replaces the data 'whatever' with
# 'shoelace'
$cache->replace(7, 'tattoo'); # fails key/value pair was never set() or
# add()ed previously
my $h = $cache->get(1); # $h and \%some_hash contain the same data
my $bar = $cache->get('foo'); # $bar eq 'bar';
my $obj = $cache->get(20); # returns the blessed object
$cache->count() == 3;
# assuming 10 seconds has passed ...
$cache->is_expired(20); # returns true ..
$cache->purge();
$cache->get(20); # returns undef
$cache->count() == 2;
my $hr = $cache->get_bulk();
# $hr = {1 => {contents_of => '%some_hash'},
# 21 => 'shoelace' };
$cache->close(); # close the cache object
=head1 DESCRIPTION
This module implements a caching layer around BerkeleyDB
for object persistence. It implements the basic methods necessary to
add, retrieve, and remove objects. The main advantage over other
caching modules is performance. I've attempted to stick with a
B<Cache::Cache>-like interface as much as possible, though it may differ
here and there.
=head1 DEPENDENCIES
I've been developing using a very recent version of Berkeley DB
(v4.4.20) and BerkeleyDB (v0.27). I'm pretty sure that most of the
functionality the module relies on is available in Berkeley DB version
3 and higher, but so far I have not tested with older versions. I'm
open to making version specific concessions if necessary. If at all
possible, I would advise you to upgrade both Berkeley DB and
BerkeleyDB to their latest respective versions.
Cache::BDB currently serializes everything it stores with Storable.
=head1 PERFORMANCE
The intent of this module is to supply great performance with a
reasonably feature rich API. There is no way this module can compete
with, say, using BerkeleyDB directly, and if you don't need any kind
of expiration, automatic purging, etc, that will more than likely be
much faster. If you'd like to compare the speed of some other caching
modules, have a look at
B<http://cpan.robm.fastmail.fm/cache_perf.html>. I've included a
patch which adds Cache::BDB to the benchmark.
=head1 LOCKING
All Cache::BDB environments are opened with the DB_INIT_CDB
flag. This enables multiple-reader/single-writer locking handled
entirely by the Berkeley DB internals at either the database or
environment level. See
http://www.sleepycat.com/docs/ref/cam/intro.html for more information
on what this means for locking.
Important: it is a bad idea to share a single Cache::BDB object across
multiple processes or threads. Doing so is bound to cause you
pain. Instead, have your thread/process instantiate its own Cache::BDB
object. It is safe to have them all pointing at the same cache file.
=head1 CACHE FILES
For every new B<Cache::BDB> object, a Berkeley DB Environment is
created (or reused if it already exists). This means that even for a
single cache object, at least 4 files need to be created, three for
the environment and at least one for the actual data in the cache. Its
possible for mutliple cache database files to share a single
environment, and its also possible for multiple cache databases to
share a single database file. See the SYNOPSIS above for a quick view
of what you are likeley to find on the filesystem for a
cache. Cache::BDB uses BerkeleyDB exclusively with regard to files, so
if you have questions about whats in those files, you might
familiarize yourself further with Berkeley DB.
=head1 USAGE
=over 4
=item B<new>(%options)
=over 4
=item * cache_root
Specify the top level directory to store cache and related files
in. This parameter is required. Keep in mind that B<Cache::BDB> uses a
B<BerkeleyDB> environment object so more than one file will be written
for each cache.
=item * cache_file
If you want to tell B<Cache::BDB> exactly which file to use for your
cache, specify it here. This parameter is required if you plan to use
the env_lock option and/or if you want to have multiple logical
databases (namespaces) in a single physical file. If unspecified,
B<Cach::BDB> will create its database file using the
B<namespace>. B<cache_file> should be relative to your cache_root, not
fully-qualified, i.e.
my %options = ( cache_root => '/some/location/for/caching/',
cache_file => 'whatever.db',
namespace => 'MyObjects');
This gives you, among other files, /some/location/for/caching/whatever.db.
Your logical database inside of 'whatever.db' will be named with 'MyObject'.
If you were to then instantiate another Cache::BDB with the following:
my %options = ( cache_root => '/some/location/for/caching/',
cache_file => 'whatever.db',
namespace => 'MyOtherObjects');
You would now have two logical caches in one physical file, which is
ok, but see B<namespace> below for a better idea.
=item * namespace
Your B<namespace> tells B<Cache::BDB> where to store cache data under
the B<cache_root> if no B<cache_file> is specified or what to call the
database in the multi-database file if B<cache_file> is specified. It
is a required parameter. For clarity, it might be best to instantiate
B<Cache::BDB> objects like so:
my $namespace = 'MyObjects';
my %options = ( cache_root => "/some/location/for/caching/$namespace",
namespace => $namespace );
Unlike the examples given above under cache_file, this allows you to
locate a single cache type in its own directory, which gives you more
flexibility to nuke it wholesale or move things around a little.
=item * type
Cache::BDB allows you to select the type of Berkeley DB storage
mechanism to use. Your choices are Hash, Btree, and Recno. Queue isn't
supported. I haven't tested the three supported types extensively. The
default, if unspecified, is Btree, and this is probably good enough
for most applications. Note that if a cache is created as one type it
must remain that type. If you instantiate a Cache::BDB object with one
type (or use the default), and then attempt to connect to the same
cache with a newly instantiated object that uses a different type, you
will get a warning, and Cache::BDB will be nice and connect you to the
cache with its original type.
Important: up until Berkeley DB 4.4.x, it has not been possible to
shrink the physical size of a database file, which means that,
technically, your cache files will never get smaller even if you
delete everything from them. HOWEVER, with 4.4.x this functionality is
now possiblye but it will only work with the Btree type. As soon as
this is available in the BerkeleyDB.pm wrapper (soon I'm told), I'll
be releasing a version with some options to allow this. Point being,
this may be a good reason to stick with Btree.
For more info, see http://www.sleepycat.com/docs/ref/am_conf/intro.html.
=item * env_lock
If multiple databases (same or different files) are opened using the
same Berkeley DB environment, its possible to turn on environment
level locking rather than file level locking. This may be advantageous
if you have two separate but related caches. By passing in the
env_lock parameter with any true value, the environment will be
created in such a way that any databases created under its control
will all lock whenever Berkeley DB attempts a read/write lock. This
flag must be specified for every database opened under this
environment. Note: this is very untested in Cache::BDB, and I don't
know how necessary it is.
=item * default_expires_in
Time (in seconds) that cached objects should live. If set to 0,
objects never expire. See B<set> to enable a per-object value.
=item * auto_purge_interval
Time (in seconds) that the cached objects will be purged by one or
both of the B<auto_purge> types (get/set). If set to 0, auto purge is
disabled. Note, of course, that objects won't actually be purged until
some event actually takes place that will call purge (set or get), so
if this is set to 300 but no gets or sets are called for more than 300
seconds, the items haven't actually been purged yet.
=item * auto_purge_on_set
If this item is true and B<auto_purge_interval> is greater than 0,
calling the B<set> method will first purge any expired records from
the cache.
=item * auto_purge_on_get
If this item is true and B<auto_purge_interval> is greater than 0,
calling the B<get> method will first purge any expired records from
the cache.
=item * purge_on_init
If set to a true value, purge will be called before the constructor returns.
=item * purge_on_destroy
If set to a true value, purge will be called before the object goes
out of scope.
=item * clear_on_init
If set to a true value, clear will be called before the constructor returns.
=item * clear_on_destroy
If set to a true value, clear will be called before the object goes
out of scope.
=item * disable_compact
Disable database compactions for clear, purge, delete and remove
methods. See B<DATABASE SIZE> below for more information on database
compaction.
=item * disable_auto_purge
As a courtesy, Cache::BDB will automatically remove() any expired
cache item you get() before returning undef. This is handy if you
don't feel the need to do a lot of explicit cache purging, but if you
only want purge, remove, delete or clear to actually delete cache
items, you can disable this functionality by passing in
disable_auto_purge with any true value.
=back
=back
=over 4
=item B<close>()
Explicitly close the connection to the cache. A good idea. Essentially
the same as undef'ing the object (explicitly calls DESTROY).
=item B<namespace>()
This read only method returns the namespace that the cache object is
currently associated with.
=item B<auto_purge_interval>($seconds)
Set/get the length of time (in seconds) that the cache object will
wait before calling one or both of the B<auto_purge> methodss. If set
to 0, automatic purging is disabled.
=item B<auto_purge_on_set>(1/0)
Enable/disable auto purge when B<set> is called.
=item B<auto_purge_on_get>(1/0)
Enable/disable auto purge when B<get> is called.
=item B<set>($key, $value, [$seconds])
Store an item ($value) with the associated $key. Time to live (in
seconds) can be optionally set with a third argument. Returns true on success.
=item B<add>($key, $value, [$seconds])
Only B<set> in the cache if the key doesn't already exist.
=item B<replace>($key, $value, [$seconds])
Only B<set> in the cache if the key does exist.
=item B<get>($key)
Locate and return the data associated with $key. Returns the object
associated with $key or undef if the data doesn't exist. If
B<auto_purge_on_get> is enabled, the cache will be purged before
attempting to locate the item.
=item B<get_bulk>()
Returns a hash reference containing every unexpired item from the
cache key'ed on their cache id. This can be useful if your keys aren't
always available or if you just want to use the cache as a convenient
way to dump data in chunks.
The result looks something like this:
my $h = $cache->get_bulk();
# $h = { 123 => "bird and bee",
# 456 => "monkeys with sticks",
# 789 => "take whats mine",
# };
=item B<remove>($key)
Removes the cache element specified by $key if it exists. Returns true
for success.
=item B<delete>($key)
Same as remove()
=item B<clear>()
Completely clear out the cache and compact the underlying
database. Returns the number of cached items removed.
=item B<count>()
Returns the number of items in the cache.
=item B<size>()
Return the size (in bytes) of all the cached items. This call relies
on the availability of B<Devel::Size>. If its not found, you'll get a
warning and size() will simply return 0. Currently the size is
calculated every time this is called by using
B<Devel::Size::total_size>, so it may be expensive for large
caches. In the future size-aware options and functionality may be
available, but for now you'll need to implement this outside of
Cache::BDB if you need it.
=item B<purge>()
Purge expired items from the cache. Returns the number of items purged.
=item B<is_expired>($key)
Returns true if the data pointed to by $key is expired based on its
stored expiration time. Returns false if the data isn't expired *or* if the
data doesn't exist.
=back
=head1 DATABASE SIZE
(See http://www.sleepycat.com/docs/ref/am_misc/diskspace.html)
Before Berkeley DB release 4.4 it was not possible to return freed
space in a database file. This means that no matter how many items you
delete, your file will still retain its size, and continue to grow as
you add more items. The only way to get the file size back down was to
dump the database to a file and reload it into a new database
file. This may or may not be a problem for your application, but keep
in mind that your cache will continue to get bigger and, for example,
your operating system may have a maximum file size limit.
In 4.4, Sleepycat introduced the ability to free unused
space. BerkeleyDB 0.29 exposes this functionality in the perl
wrapper. If you are using these versions or better and have chosen the
Btree database type (the default for Cache::BDB), your caches will
automatically be compacted when items are purged, removed/deleted, or
if clear is called. You can disable the automatic compaction of cache
files by initializing your Cache::BDB object with the disable_compact
parameter set to any true value. In my tests so far, however, database
compaction does not appear to affect performance significantly, and
may save you from a headache down the road.
=head1 AUTHOR
Josh Rotenberg, C<< <joshrotenberg at gmail.com> >>
=head1 TODO
* Make data storage scheme configurable (Storable, YAML, Data::Dumper,
or callback based)
* Split storage between meta and data for faster operations on meta data.
* Add some size/count aware features.
* Create some examples.
* Fix fork()'ing tests.
=head1 BUGS
Please report any bugs or feature requests to C<bug-cache-bdb at
rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Cache-BDB>. I will
be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Cache::BDB
You can also look for information at:
=over 4
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Cache-BDB>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Cache-BDB>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Cache-BDB>
=item * Search CPAN
L<http://search.cpan.org/dist/Cache-BDB>
=back
=head1 SEE ALSO
BerkeleyDB
=head1 ACKNOWLEDGEMENTS
Baldur Kristinsson
Sandy Jensen
=head1 COPYRIGHT & LICENSE
Copyright 2006 Josh Rotenberg, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
|