{"id":18698286,"url":"https://github.com/ihor/cachalot","last_synced_at":"2026-02-25T07:38:40.605Z","repository":{"id":6407948,"uuid":"7646174","full_name":"ihor/Cachalot","owner":"ihor","description":"Caching rethought – cache a lot in a proper way.","archived":false,"fork":false,"pushed_at":"2021-04-21T11:26:02.000Z","size":56,"stargazers_count":25,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-01T10:37:08.721Z","etag":null,"topics":["apcu","cache","couchbase","memcache","php","redis","xcache"],"latest_commit_sha":null,"homepage":"http://cachalot.readthedocs.org","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ihor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-16T13:28:49.000Z","updated_at":"2023-03-23T12:24:37.000Z","dependencies_parsed_at":"2022-07-26T01:02:26.818Z","dependency_job_id":null,"html_url":"https://github.com/ihor/Cachalot","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihor%2FCachalot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihor%2FCachalot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihor%2FCachalot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihor%2FCachalot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ihor","download_url":"https://codeload.github.com/ihor/Cachalot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223505021,"owners_count":17156448,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apcu","cache","couchbase","memcache","php","redis","xcache"],"created_at":"2024-11-07T11:27:40.531Z","updated_at":"2026-02-25T07:38:40.510Z","avatar_url":"https://github.com/ihor.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Cachalot\n========\n\nCachalot (cache a lot) is an easy to use caching library. It supposed to do the only one thing - return cached callback result.\n\nInstallation\n------------\nDefine the following requirement in your composer.json file:\n```\n\"require\": {\n    \"ihor/cachalot\": \"2.3\"\n}\n```\n\nUsage\n-----\n```php\n// With Cachalot cache you can easily cache results of different types of functions\n$cache = new \\Cachalot\\ArrayCache();\n\n// built-in functions\n$length = $cache-\u003egetCached('strlen', ['hello world']); \n\n// user defined functions\n$unique = $cache-\u003egetCached('uniqueValues', [[1, 2, 3, 1, 2, 3]]);\n\n// static methods\n$result = $cache-\u003egetCached(['Calculator', 'subtract'], [1, 2]);\n\n// instance methods\n$square = $cache-\u003egetCached([new Calculator(), 'square'], [5]);\n\n// anonymous functions\n$reason = $cache-\u003egetCached($getErrorReason, [], \\Cachalot\\Cache::ONE_DAY, 'error-reason');\n\n// callable objects\n$trimed = $cache-\u003egetCached(new Trimmer(), [' hello world ']);\n```\n\nReference\n---------\n### Cache API\n\n##### getCached($callback, array $args = array(), $expireIn = 0, $suffix = null, $useSuffixAsKey = false)\n\nReturns cached $callback result\n\n```$callback``` is the function (callable) which results we want to be cached  \n```$args``` are the arguments passed to the ```$callback```  \n```$expireIn``` sets cache TTL in seconds  \n```$suffix``` is needed to avoid collisions when callback is an anonymous function\n```$useSuffixAsKey``` when true cache suffix will be used as a cache key\n\n```php\n$length = $cache-\u003egetCached('strlen', ['hello world']);\n```\n\nTo have possibility to use Cachalot as a regular caching library when needed it contains classic cache methods\n\n##### contains($key)\n\nReturns true if cache contains entry with given key\n\n```php\nif ($cache-\u003econtains('lastVisit')) {\n    echo 'This is not the first visit';\n}\n```\n\n##### get($key)\n\nReturns cached value by key or false if there is no cache entry for the given key\n\n```php\nif ($lastVisitDate = $cache-\u003eget('lastVisit')) {\n    echo sprintf('Last visit was at %s', $lastVisitDate);\n}\n```\n\n##### set($key, $value, $expireIn = 0)\n\nCaches value by key. When ```$expireIn = 0``` the value is cached forever\n\n```php\n$cache-\u003eset('lastVisit', time());\n```\n\n##### delete($key)\n\nDeletes cache entry by key\n\n```php\n$cache-\u003edelete('lastVisit');\n```\n\n##### clear()\n\nDeletes all cache entries\n\n```php\n$cache-\u003eclear();\n```\n\n### Back-ends\n\n##### Cachalot\\ApcCache\n\nStores data in [APC](http://php.net/manual/en/book.apc.php)\n\n```php\n$cache = new Cachalot\\ApcCache();\n```\n\n##### Cachalot\\XcacheCache\n\nStores data in [Xcache](https://xcache.lighttpd.net/)\n\n```php\n$cache = new Cachalot\\XcacheCache();\n```\n\n##### Cachalot\\MemcacheCache\n\nStores data in [Memcached](http://memcached.org) using [Memcache PHP extension](http://php.net/manual/en/book.memcache.php) \n\n```php\n$memcache = new \\Memcache();\n$memcache-\u003econnect('unix:///usr/local/var/run/memcached.sock', 0);\n\n$cache = new \\Cachalot\\MemcacheCache($memcache);\n```\n\n##### Cachalot\\MemcachedCache\n\nStores data in [Memcached](http://memcached.org) using [Memcached PHP extension](http://php.net/manual/en/book.memcached.php)\n\n```php\n$memcached = new \\Memcached();\n$memcached-\u003eaddServer('/usr/local/var/run/memcached.sock', 0);\n\n$cache = new \\Cachalot\\MemcachedCache($memcached);\n```\n\n##### Cachalot\\RedisCache\n\nStores data in [Redis](http://redis.io)\n\n```php\n$redis = new \\Redis();\n$redis-\u003econnect('127.0.0.1');\n$redis-\u003eselect(1);\n\n$cache = new \\Cachalot\\RedisCache($redis);\n```\n\n##### Cachalot\\CouchbaseCache\n\nStores data in [Couchbase](http://www.couchbase.com/) using [Couchbase PHP SDK 1.x](http://docs.couchbase.com/couchbase-sdk-php-1.2/index.html)\n\n```php\n$couchbase = new \\Couchbase('127.0.0.1', '', '', 'default');\n\n$cache = new \\Cachalot\\CouchbaseCache($couchbase);\n```\n\n##### Cachalot\\Couchbase2Cache\n\nStores data in [Couchbase](http://www.couchbase.com/) using [Couchbase PHP SDK 2.x](http://developer.couchbase.com/documentation/server/4.0/sdks/php-2.0/php-intro.html)\n\n```php\n$cluster = new \\CouchbaseCluster('couchbase://localhost');\n$bucket = $cluster-\u003eopenBucket('default');\n\n$cache = new \\Cachalot\\Couchbase2Cache($bucket);\n```\n\n##### Cachalot\\ArrayCache\n\nStores data in PHP array\n\n```php\n$cache = new \\Cachalot\\ArrayCache();\n```\n\n##### Cachalot\\BlackholeCache\n\nNever stores any data\n\n```php\n$cache = new \\Cachalot\\BlackholeCache();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fihor%2Fcachalot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fihor%2Fcachalot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fihor%2Fcachalot/lists"}