emileperron/magic-cache

An easy to use caching library that uses Symfony's FilesystemAdapter under the hood.

v1.1.2 2020-11-21 22:25 UTC

This package is auto-updated.

Last update: 2024-11-13 10:55:30 UTC


README

MagicCache is a simple caching library that wraps Symfony's FilesystemAdapter in order to give you the simplest caching experience possible.

If you have a small project that requires caching, or have a large project with very simple caching needs, this library is for you.

The highlight of this library are the Cache::magicGet() and Cache::magicSet() method, which allow you to save or retrieve data from the cache without having to define unique cache keys yourself. More details on that below.

Getting started

To get started, simply require the package via Composer:

composer require emileperron/magic-cache

Once that's done, you can start using the library in your project. Here's a very brief overview of the library's usage and functionalities:

<?php

use Emileperron\MagicCache\Cache;

// Saving data to cache
// Definition: set($key, $value, $expiresAfter = 0)
Cache::set('my_cache_key', 'my value');
Cache::set('my_cache_key_2', 'my second value', 3600);
Cache::set('my_cache_key_3', ['obj' => 'value']);

// Getting data from cache
// Definition: get($key, $fallback = null)
Cache::get('my_cache_key');
Cache::get('my_non_existing_key', 'fallback value');
// The $fallback parameter also allows for callables
Cache::get('my_non_existing_key', 'MyClass::myCallbackMethod');
Cache::get('my_non_existing_key', ['MyClass', 'myCallbackMethod']);
Cache::get('my_non_existing_key', function(){
	return 'fallback value';
});

// Manually removing data from cache
// Definition: remove($key)
Cache::remove('my_cache_key_3');

// Pruning the cache (deletes all expired cache entries)
// Definition: prune()
Cache::prune();

// This library defines two magic methods: magicGet() and magicSet()
// These methods automatically generate a cache key based on the calling function's class, name and parameters.
// Here's an example:
function getRecordFromDatabase($id)
{
	return Cache::magicGet([], function() use ($id) {
		$value = fetchValueFromDatabase($id); // replace this with your own data fetching/processing code
		return Cache::magicSet($value);
	});
}

// The definition of both methods goes as follows:
// magicGet($fallback = null, $suffixes = [])
// magicSet($value, $expiresAfter = 0, $suffixes = [])

Magic get() and set() methods

This library defines two magic methods: magicGet() and magicSet(). These methods automatically generate a cache key based on the calling function's class, name and parameters.

This won't always be usable (ex.: if you are passing entire files or data stream in your function's parameters), but it works for a lot of simple use cases, which are much more common anyway.

Both methods accept a $suffixes parameter, which allow you to pass your own unique identifier(s) in the form of an array. Those suffixes will then be used instead of the calling function's parameters. The calling function's class and name will still be used in order to generate a unique cache key for that specific method.

Contributing

Feel free to submit pull requests on the GitHub repository if you want to add functionalities or suggest improvements to this library. I will look them over and merge them as soon as possible.

You can also submit issues if you run into problems but don't have time to implement a fix.

Supporting

Finally, if you use the library and would like to support me, here are the ways you can do that: