July 21, 2010

I’ve developed a new class for WinCache that wraps around the wincache_ucache* functions. The goal of this project is to improve and add to the functionality of WinCache’s User Cache feature.

Basic Usage

Below is a quick code example of how this class simplifies the way in which a developer can take advantage of the user cache.

/**
 * Get Cache Object
 */
$cacheObj = DDWinUCache::getInstance();
/**
 * Cache Data
 */
$cacheObj->set('foo', 'bar');
$cacheObj->set('bar', 'candy');
$cacheObj->set('hello_world', 'we did it!');
$cacheObj->set('apple_red_core', 'simple tagging');
/**
 * Delete Cache Entries
 */
$cacheObj->delete('*or*'); // array('apple_red_core', 'hello_world');
/**
 * Get Cached Data
 */
$cacheObj->get('foo'); // "bar"

New Features

Within this class I’ve greatly improved the functionality of the delete method. I’ve also made getting information about the user cache simpler by creating accessor methods for all user cache related data. And to top it all off I’ve implemented ArrayAccess to push the simplicity over the edge.

Improved Delete

Arguably the biggest feature of this class is the ability to delete cache entries via three methods.

/**
 * Traditional
 */
$cacheObj->delete('this_is_a_test_entry');
/**
 * Wild Cards '*' and '?'
 */
$cacheObj->delete('this*');
$cacheObj->delete('this?is?a?test?entry');
/**
 * Regular Expression (PCRE)
 */
$cacheObj->delete('/^this.*?entry$/i');

### Accessor Methods
I’ve mapped all the data available via the arrays returned by wincache_ucahce_info() and wincache_ucahce_meminfo() into accessor methods. I’ve also added two more derived methods that can be used to help control the user cache – getMemoryUsed() and getMemoryUsedPercent().

ArrayAccess

The code below accomplishes the same tasks as in the *Basic Usage example above…

/**
 * Get Cache Object
 */
$cacheObj = DDWinUCache::getInstance();
/**
 * Cache Data
 */
$cacheObj['foo'] = 'bar';
$cacheObj['bar'] = 'candy';
$cacheObj['hello_world'] = 'we did it!';
$cacheObj['apple_red_core'] = 'simple tagging';
/**
 * Delete Cache Entries
 */
unset($cacheObj['*or*']); // array('apple_red_core', 'hello_world');
/**
 * Get Cached Data
 */
$cacheObj['foo'] // "bar"

Garbage Collection Issue

WinCache’s User Cache has a maximum memory limit of 85 Mb. With this cap one must be careful as WinCache has no built-in garbage collection outside of the TTL settings on each cache entry. This lack of garbage collection becomes an issue when one tries to set a cache variable while the user cache’s memory usage is maxed out – as the operation will fail.

This situation can be avoided by using getMemoryUsedPercent() in conjunction with clear() or delete(...).

Summary

My objective was to build a mapper class that extended the functionality of the WinCache User Cache. Specifically, I focused on making entry deletion more versatile and increasing the simplicity by which one interacted with the cache. To those ends I believe this is a great solution.

If you have any feature requests or bug reports please let me know and I’ll add/fix them asap.

License

This class and its code is released under the New-BSD License.

Download

The source code is available to anyone at http://github.com/veloper/DDWinUCache.

Tail Multiple Logs with Capistrano

Here’s a quick and easy way to tail log files from multiple hosts using Capistrano and the Foreman gem.Capistrano TaskFirst you’ll need to make sure the fore... Continue reading

Add Eloquent ORM Tab to PHP Debug Bar

Published on August 30, 2015

Doctrine 2 PDO Object

Published on August 17, 2015