Skip to content

LeagueAPI: Cache providers

Daniel Dolejška edited this page Oct 12, 2019 · 5 revisions

Version v4.0.1

Cache providers are responsible for keeping data of rate limiting, call caching and static data within instances of the library for easy re-use. This feature is automatically enabled, when any of previously mentioned features is used.

When using this feature, you can set LeagueAPI::SET_CACHE_PROVIDER to any class, thought it has to be compatible with PSR-6 standard, eg. implement Psr\Cache\CacheItemPoolInterface interface. By using LeagueAPI::SET_CACHE_PROVIDER_PARAMS or LeagueAPI::SET_DD_CACHE_PROVIDER_PARAMS option, you can pass any data to the cache provider class.

If no custom cache provider class is specified (via LeagueAPI::SET_CACHE_PROVIDER) default filesystem caching is used. System's temp directory (via sys_get_temp_dir()) is used as a base directory for this type of caching by default.

Settings key Data type Info / Possible values
LeagueAPI::SET_CACHE_PROVIDER Psr\Cache\CacheItemPoolInterface See section built-in providers
LeagueAPI::SET_CACHE_PROVIDER_PARAMS array see examples below
LeagueAPI::SET_DD_CACHE_PROVIDER_PARAMS array see examples below

Library initialization

FilesystemAdapter

Implementation of Symfony FilesystemAdapter (Symfony docs) accepts 3 arguments - namespace, defaultLifetime, directory.

use RiotAPI\LeagueAPI\Definitions\Cache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

// Cache provider control class
$cacheProvider = FilesystemAdapter::class;
// Cache provider params for LeagueAPI library instance
$leagueapiParams = [
	Cache::LEAGUEAPI_NAMESPACE, // cache namespace
	Cache::LIFETIME,            // default lifetime
	Cache::getDirectoryPath()   // target cache directory
];
// Cache provider params for DataDragonAPI library instance
$datadragonParams = [
	Cache::DATADRAGON_NAMESPACE, // cache namespace
	Cache::LIFETIME,             // default lifetime
	Cache::getDirectoryPath()    // target cache directory
];

These values will be set in LeagueAPI::SET_CACHE_PROVIDER_PARAMS and LeagueAPI::SET_DD_CACHE_PROVIDER_PARAMS and passed down to the class upon its initialization.

use RiotAPI\LeagueAPI\LeagueAPI;

$api = new LeagueAPI([
	// ...
	LeagueAPI::SET_CACHE_PROVIDER           => $cacheProvider,
	LeagueAPI::SET_CACHE_PROVIDER_PARAMS    => $leagueapiParams,
	LeagueAPI::SET_DD_CACHE_PROVIDER_PARAMS => $datadragonParams,
	// ...
]);

MemcachedAdapter

Let's set up \Memcached client first (it is required by the cache provider class). For more information about configuring the connection, please visit Symfony docs.

use Symfony\Component\Cache\Adapter\MemcachedAdapter;

$memcachedClient = MemcachedAdapter::createConnection([
	'memcached://janesmith:mypassword@/var/run/memcached.sock',
	[
		'compression' => true,
		// ...
	]
]);

Implementation of Symfony MemcachedAdapter (Symfony docs) accepts 3 arguments - memcachedClient, namespace, defaultLifetime. Since we already created the client before, we just need to set up the parameter array (just like before):

use RiotAPI\LeagueAPI\Definitions\Cache;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;

// Cache provider control class
$cacheProvider = MemcachedAdapter::class;
// Cache provider params for LeagueAPI library instance
$leagueapiParams = [
	$memcachedClient,           // Memcached client
	Cache::LEAGUEAPI_NAMESPACE, // cache namespace
	Cache::LIFETIME,            // default lifetime
];
// Cache provider params for DataDragonAPI library instance
$datadragonParams = [
	$memcachedClient,            // Memcached client
	Cache::DATADRAGON_NAMESPACE, // cache namespace
	Cache::LIFETIME,             // default lifetime
];

These values will be set in LeagueAPI::SET_CACHE_PROVIDER_PARAMS and LeagueAPI::SET_DD_CACHE_PROVIDER_PARAMS and passed down to the class upon its initialization (just like before).

use RiotAPI\LeagueAPI\LeagueAPI;

$api = new LeagueAPI([
	// ...
	LeagueAPI::SET_CACHE_PROVIDER           => $cacheProvider,
	LeagueAPI::SET_CACHE_PROVIDER_PARAMS    => $leagueapiParams,
	LeagueAPI::SET_DD_CACHE_PROVIDER_PARAMS => $datadragonParams,
	// ...
]);

Built-in providers

For all available built-in cache providers, please visit Symfony docs.

Personal favourite selection: