khoaofgod/phpfastcache — pull request #46 by laurent22 (2014-01-07 15:44:08+00:00)
Title: Added search method to main class and to file driver

This pull request adds a `search()` method to the main phpfastcache object. I have also added support for this method to the file driver. It would be used like so:

__c()->set('module1_user_123', 'example 1');
__c()->set('module1_user_456', 'example 2');
__c()->set('module1_user_456', 'example 2');
__c()->set('module1_page_789', 'example 3');
__c()->set('module2_product_1', 'example 4');
__c()->set('module2_user_888', 'example 5');

// Returns all the objects starting with "module2_"
var_dump(__c()->search('module2_*'));

// Returns all the objects containing "_user_"
var_dump(__c()->search('*_user_*'));

// Returns all the objects (in the example above):
var_dump(__c()->search('module?_*'));

This is useful to inspect the cached data and it also allows more control over it. For example, (as in the example above), it is possible create cache groups by adding prefix to the keys. Then these groups can be updated or deleted together.

The query parameter is a simple glob pattern (with only `?` and `*` being supported) so it should be easy to add support for it to all the drivers.

One issue with the current implementation of the phpfastcache file driver is that it doesn't store the cache key, rather it encodes it as an MD5 hash (which of course cannot be reversed). So I have updated the driver so that it encodes it as a base64 string instead. It is actually faster and it allows us to search by cache key.

If there's some interest in the pull request, I'd be glad to implement the search method for the other drivers.