PHP Classes

Cache DBA: Store cache data from flat file databases

Recommend this page to a friend!
  Info   Screenshots Screenshots   View files View files (31)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-01-26 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 439 All time: 6,061 This week: 628Up
Version License PHP version Categories
cache-dba 1.3BSD License5.0PHP 5, Databases, Cache
Description Author

This class can store and retrieve cache data from flat file databases.

It takes a given object and stores its serialized information as a string of data in flat file database using the DBA extension.

The class can also retrieve and unserialize the previously stored object from the cache database file if the cache file exists and it did not expire.

It can also delete existing cache entries explicitly. A separate class exists to automatically delete all expired cache database entries.

Innovation Award
PHP Programming Innovation award nominee
March 2011
Number 4
Caching is a technique often used to save time generating only certain content once, store it somewhere and then retrieve it from storage next time it is necessary, so you do not waste time regenerating every time it is needed.

Caching methods often use individual files, databases or memcached containers to store cached content.

This class uses a slightly different type of container to store cached data: a flat file database. This way it can store many cache entries in a single file, making it fast to lookup the existence of a cache entry, while preserving the benefits of storing cached content in a local files.

Manuel Lemos
  Performance   Level  
Name: Gjero Krsteski <contact>
Classes: 4 packages by
Country: Germany Germany
Age: 38
All time rank: 1676105 in Germany Germany
Week rank: 989 Up38 in Germany Germany Up
Innovation award
Innovation award
Nominee: 2x


The php-dba-cache uses the database (dbm-style) abstraction layer to cache/store your PHP objects, 
strings, integers or arrays. Even instances of SimpleXMLElement can be put to the cache. You don`t
have to matter about the size of the cache-file. It depends on the free space of your disk.

[![Build Status](](
[![Scrutinizer Quality Score](](
[![Dependency Status](](

Available options

- Open a given dba database
- Insert a new record with a given key (persistently or with a given expiration time)
- Get a record with a given key
- Replace the value of a record with a given key
- Delete the record with a given key
- Return metadata for the given key: expire timestamp & timestamp of last modification time
- Get all keys from cache
Sweep - CacheGarbageCollector (optional)
- Clean all entries
- Clean expired entries
- Flush the cache file
- Optimizes the database file automatically after cleaning process


"By using the --enable-dba=shared configuration option you can build a dynamic loadable module
to enable PHP for basic support of dbm-style databases. You also have to add support for at
least one of the following handlers by specifying the --with-XXXX configure switch
to your PHP configure line."
More about installation:

DBA handlers

The behaviour of various aspects for the caching depends on the implementation of yor
installed dba-type database. I have tested it with several database-handlers like db4, flatfile,
cdb, cdb_make, gdbm. The cdb & cdb_make family is the fastest, but you have to create
you own garbage-collection-cleaner or you can delete it manually. Take a look at the tests
( to better understand
how to use the cache.

cdb = Tiny Constant Database - for reading
Cdb is "a fast, reliable, lightweight package for creating and reading constant databases.
" It is from the author of qmail and can be found at Since it is
constant, we support only reading operations. And since PHP 4.3.0 we support writing
(not updating) through the internal cdb library.

cdb_make = Tiny Constant Database - for writing
Since PHP 4.3.0 we support creation of cdb files when the bundled cdb library is used.

db4 = Oracle Berkeley DB 4 - for reading and writing
DB4 is Sleepycat Software's DB4. This is available since PHP 4.3.2.

gdbm = GNU Database Manager - for reading and writing
Gdbm is the GNU database manager.

flatfile = default dba extension - for reading and writing
This is available since PHP 4.3.0 for compatibility with the deprecated dbm extension only
and should be avoided. However you may use this where files were created in this format.
That happens when configure could not find any external library.
More about requirements:

Nice to know

Not all of the DBA-style databases can replace key-value pairs, like the CDB. The CDB database
can handle only with fixed key-value pairs. The best and fastest handlers for DBA-style caching
are: QDBM, Berkeley DB (DB4), NDBM and least the Flatfile.
Most problematic are dbm and ndbm which conflict with many installations. The reason for this is
that on several systems these libraries are part of more than one other library. The configuration
test only prevents you from configuring malfaunctioning single handlers but not combinations.

Sample for Oracle Berkeley DB 4 with persistent connection

$cache = new Cache(
  '/your/path/to/the/cahe-file/cache.db4', 'db4'

$yorObject            = new ObjectYouWantToPutInCache();
$yourObjectIdentifier = 'your:cool:object:identifier';

// Check if your object is in the cache.
// You also can ignore it, and let the CacheDba do it for you.
if (true === $cache->has($yourObjectIdentifier)) {

$cache->put($yourObjectIdentifier, $yorObject);

// Than somewhere at your project.

// For the garbage collection 
// you can create an cron-job starting once a day.
$sweep = new Sweep($cache);

// or clean all objects older than given expiration since now.

Saving SimpleXMLElement instances into DB 4 with persistent connection

$string = "<?xml version='1.0'?>
 <title>Let us cache</title>
 <body>Some content here</body>

$simplexml = simplexml_load_string(

$identifier = md5('simplexml_identifier');

$path = dirname(__FILE__).'/simple-xml-test-cache.db4';
$cache = new Cache($path, 'db4');

$cache->put($identifier, $simplexml);

$getObject = $cache->get($identifier);

error_log(' - PUT IN CACHE : '.print_r($simplexml, true));
error_log(' - GET FROM CACHE : '.print_r($getObject, true));

error_log(' - IS SAME OBJECT : '.
    print_r(($simplexml->asXml() === $getObject->asXml())
            ? 'true' : 'false', true));

PHP DBA Cache Monitor
Responsive web-interface that provides a comprehensive, easy to use and of course good looking GUI. It not only gives access to statistical parameters of DBA Cache, it also makes it easier to administer a web-server running DBA by providing comfortable features for optimizing the cache and deleting entries from the cache.
 ![PHP DBA cache Monitor 2014]( "PHP DBA Cache Monitor")

Benchmark Test of DBM Brothers

This benchmark test is to calculate processing time (real time)
and file size of database. Writing test is to store 1,000,000 records. Reading test is
to fetch all of its records. Both of the key and the value of each record are such 8-byte
strings as `00000001', `00000002', `00000003'... Tuning parameters of each DBM are set to
display its best performance. Platform: Linux 2.4.31 kernel, EXT2 file system,
Pentium4 1.7GHz CPU, 1024MB RAM, ThinkPad T42 Compilation: gcc 3.3.2 (using -O3), glibc 2.3.3

NAME        DESCRIPTION                             WRITE TIME  READ TIME   FILE SIZE
QDBM        Quick Database Manager 1.8.74           1.89        1.58        55257
NDBM        New Database Manager 5.1                8.07        7.79        814457
SDBM        Substitute Database Manager 1.0.2       11.32       0.00        606720
GDBM        GNU Database Manager 1.8.3              14.01       5.36        82788
TDB         Trivial Database 1.0.6                  9.64        2.22        51056
CDB         Tiny Constant Database 0.75             0.87        0.80        39065
BDB         Berkeley DB 4.4.20                      9.62        5.62        40956
QDBM-BT-ASC B+ tree API of QDBM (ascending order)   2.37        1.78        24304
QDBM-BT-RND B+ tree API of QDBM (at random)         10.90       4.82        15362
BDB-BT-ASC B+ tree API of BDB (ascending order)     3.04        3.06        27520
BDB-BT-RND B+ tree API of BDB (at random)           10.03       4.15        29120

Unit of time is seconds. Unit of size is kilo bytes. Read time of SDBM can not be calculated
because its database is broken when more than 100000 records.
  • php-cache-dba-monitor.jpg
  Files folder image Files  
File Role Description
Files folder imageapp (5 files, 1 directory)
Files folder imagesrc (4 files)
Files folder imagetests (4 files, 1 directory)
Plain text file .editorconfig Data Auxiliary data
Plain text file .scrutinizer.yml Data Auxiliary data
Plain text file .travis.yml Data Auxiliary data
Plain text file bootstrap.php Class Class source
Plain text file composer.json Data Auxiliary data
Plain text file make-million.php Class Class source
Plain text file phpunit.xml Data Auxiliary data
Plain text file README.markdown Data Auxiliary data

  Files folder image Files  /  app  
File Role Description
Files folder imageimg (2 files)
  Plain text file .htaccess Data Auxiliary data
  Plain text file bootstrap-responsive.min.css Data Auxiliary data
  Plain text file bootstrap.min.css Data Auxiliary data
  Plain text file config.php Class Class source
  Plain text file index.php Class Class source

  Files folder image Files  /  app  /  img  
File Role Description
  Image file glyphicons-halflings-white.png Icon Icon image
  Image file glyphicons-halflings.png Icon Icon image

  Files folder image Files  /  src  
File Role Description
  Plain text file Cache.php Class Class source
  Plain text file Capsule.php Class Class source
  Plain text file Pack.php Class Class source
  Plain text file Sweep.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageHandlers (8 files)
  Plain text file CacheDefaultTest.php Test Unit test script
  Plain text file DummyFixtures.php Class Class source
  Plain text file PackTest.php Class Class source
  Plain text file SweepTest.php Class Class source

  Files folder image Files  /  tests  /  Handlers  
File Role Description
  Plain text file CdbReadTest.php Class Class source
  Plain text file CdbWriteTest.php Class Class source
  Plain text file Db4Test.php Class Class source
  Plain text file FlatfileTest.php Class Class source
  Plain text file GdbmTest.php Class Class source
  Plain text file InifileTest.php Class Class source
  Plain text file QdbmTest.php Class Class source
  Plain text file TestCase.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:6,061
This week:628Up