{"id":16272496,"url":"https://github.com/greg-md/php-cache","last_synced_at":"2025-04-08T15:49:31.983Z","repository":{"id":62512558,"uuid":"70004563","full_name":"greg-md/php-cache","owner":"greg-md","description":"A powerful Cache for PHP.","archived":false,"fork":false,"pushed_at":"2019-07-24T08:58:53.000Z","size":60,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-14T12:24:54.984Z","etag":null,"topics":["cache","greg-md","greg-php","php","php-cache","web-artisans"],"latest_commit_sha":null,"homepage":"","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/greg-md.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2016-10-04T20:42:50.000Z","updated_at":"2019-07-24T08:58:55.000Z","dependencies_parsed_at":"2022-11-02T13:02:07.675Z","dependency_job_id":null,"html_url":"https://github.com/greg-md/php-cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greg-md%2Fphp-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greg-md","download_url":"https://codeload.github.com/greg-md/php-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247876877,"owners_count":21011142,"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":["cache","greg-md","greg-php","php","php-cache","web-artisans"],"created_at":"2024-10-10T18:17:59.971Z","updated_at":"2025-04-08T15:49:31.961Z","avatar_url":"https://github.com/greg-md.png","language":"PHP","readme":"# Greg PHP Cache\n\n[![StyleCI](https://styleci.io/repos/70004563/shield?style=flat)](https://styleci.io/repos/70004563)\n[![Build Status](https://travis-ci.org/greg-md/php-cache.svg)](https://travis-ci.org/greg-md/php-cache)\n[![Total Downloads](https://poser.pugx.org/greg-md/php-cache/d/total.svg)](https://packagist.org/packages/greg-md/php-cache)\n[![Latest Stable Version](https://poser.pugx.org/greg-md/php-cache/v/stable.svg)](https://packagist.org/packages/greg-md/php-cache)\n[![Latest Unstable Version](https://poser.pugx.org/greg-md/php-cache/v/unstable.svg)](https://packagist.org/packages/greg-md/php-cache)\n[![License](https://poser.pugx.org/greg-md/php-cache/license.svg)](https://packagist.org/packages/greg-md/php-cache)\n\nA powerful Cache for PHP.\n\n# Table of Contents:\n\n* [Requirements](#requirements)\n* [Supported Drivers](#supported-drivers)\n* [How It Works](#how-it-works)\n* [Cache Strategy](#cache-strategy)\n* [License](#license)\n* [Huuuge Quote](#huuuge-quote)\n\n# Requirements\n\n* PHP Version `^7.1`\n\n# Supported Drivers\n\n- **Array** - Use the PHP array as a storage;\n- **Tmp** - Use temporary files as a storage. They are automatically removed when script ends;\n- **File** - Use file based storage;\n- **Memcached** - Use Memcached driver as a storage;\n- **Redis** - Use Redis driver as a storage;\n- **Sqlite** - Use Sqlite database as a storage.\n\n# How It Works\n\nThere are two ways of working with cache strategies.\nDirectly or via a cache manager.\n\n\u003e A cache manager could have many cache strategies and a default one.\n\u003e The cache manager implements the same cache strategy and could act as default one if it's defined.\n\nIn the next example we will use a cache manager.\n\n**First of all**, you have to initialize a cache manager and register some strategies:\n\n```php\n$manager = new \\Greg\\Cache\\CacheManager();\n\n// Register a file cache\n$manager-\u003eregisterStrategy('store1', new \\Greg\\Cache\\FileCache(__DIR__ . '/storage'));\n\n// Register a sqlite cache\n$manager-\u003eregister('store2', function() {\n    $pdo = new \\PDO('sqlite:' . __DIR__ . '/storage/store2.sqlite');\n\n    return new \\Greg\\Cache\\SqliteCache($pdo);\n});\n\n// Register a redis cache\n$manager-\u003eregister('store3', function() {\n    $redis = new \\Redis();\n\n    $redis-\u003econnect('127.0.0.1');\n\n    return new \\Greg\\Cache\\RedisCache($redis);\n});\n```\n\n**Optionally**, you can define a default store to be used by the cache manager.\n\n```php\n$manager-\u003esetDefaultStoreName('store2');\n```\n\n**Then**, you can **set** or **get** some data:\n\n```php\n// Add some data in \"store1\"\n$manager-\u003estore('store1')-\u003eset('foo', 'FOO');\n\n// Add some data in default store, which is \"store2\"\n$manager-\u003eset('bar', 'BAR');\n\n// Get \"bar\" value from default store.\n$value = $manager-\u003eget('bar'); // result: BAR\n```\n\n# Cache Strategy\n\nIf you want, you can create your own strategies.\nThey should implement the `\\Greg\\Cache\\CacheStrategy` interface.\n\nBelow you can find a list of **supported methods**.\n\n* [has](#has) - Determines whether an item is present in the cache;\n* [hasMultiple](#hasmultiple) - Determines whether multiple items are present in the cache;\n* [get](#get) - Fetch a value from the cache;\n* [getMultiple](#getmultiple) - Obtains multiple cache items by their unique keys;\n* [set](#set) - Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time;\n* [setMultiple](#setmultiple) - Persists a set of `key =\u003e value` pairs in the cache, with an optional TTL;\n* [forever](#forever) - Persists forever data in the cache, uniquely referenced by a key;\n* [foreverMultiple](#forevermultiple) - Persists forever a set of `key =\u003e value` pairs in the cache;\n* [delete](#delete) - Delete an item from the cache by its unique key;\n* [deleteMultiple](#deletemultiple) - Delete multiple items from the cache by their unique keys;\n* [clear](#clear) - Clear the storage;\n* [remember](#remember) - Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist;\n* [increment](#increment) - Increment a value;\n* [decrement](#decrement) - Decrement a value;\n* [incrementFloat](#incrementfloat) - Increment a float value;\n* [decrementFloat](#decrementfloat) - Decrement a float value;\n* [touch](#touch) - Set a new expiration on an item;\n* [pull](#pull) - Retrieve and delete an item from the cache;\n* [add](#add) - Persists data in the cache if it's not present.\n\n## has\n\nDetermines whether an item is present in the cache.\n\n```php\nhas(string $key): bool\n```\n\n`$key` - The cache item key.\n\n_Example:_\n\n```php\n$strategy-\u003ehas('foo');\n```\n\n## hasMultiple\n\nDetermines whether an item is present in the cache.\n\n```php\nhasMultiple(array $keys): bool\n```\n\n`$keys` - The cache items keys.\n\n_Example:_\n\n```php\n$strategy-\u003ehasMultiple(['foo', 'bar']);\n```\n\n## get\n\nFetch a value from the cache.\n\n```php\nget(string $key, mixed $default = null): mixed\n```\n\n`$key` - The unique key of this item in the cache;  \n`$default` - Default value to return if the key does not exist.\n\nReturn the value of the item from the cache, or `$default` in case of cache miss.\n\n_Example:_\n\n```php\n$strategy-\u003eget('foo');\n```\n\n## getMultiple\n\nObtains multiple cache items by their unique keys.\n\n```php\ngetMultiple(array $keys, $default = null): mixed\n```\n\n`$keys` - A list of keys that can obtained in a single operation;  \n`$default` - Default value to return for keys that do not exist.\n\nReturn a list of `key =\u003e value` pairs. Cache keys that do not exist or are stale will have `$default` as value.\n\n_Example:_\n\n```php\n$strategy-\u003eget('foo');\n```\n\n## set\n\nPersists data in the cache,\nuniquely referenced by a key with an optional expiration TTL time.\n\n```php\nset(string $key, $value, ?int $ttl = null): $this\n```\n\n`$key` - The key of the item to store;  \n`$value` - The value of the item to store, must be serializable;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.  \n\n_Example:_\n\n```php\n$strategy-\u003eset('foo', 'FOO');\n```\n\n## setMultiple\n\nPersists a set of `key =\u003e value` pairs in the cache, with an optional TTL.\n\n```php\nsetMultiple(array $values, ?int $ttl = null): $this\n```\n\n`$values` - A list of `key =\u003e value` pairs for a multiple-set operation;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.  \n\n_Example:_\n\n```php\n$strategy-\u003esetMultiple(['foo' =\u003e 'FOO', 'bar' =\u003e 'BAR']);\n```\n\n## forever\n\nPersists forever data in the cache, uniquely referenced by a key.\n\n```php\nforever(string $key, $value): $this\n```\n\n`$key` - The key of the item to store;  \n`$value` - The value of the item to store, must be serializable.\n\n_Example:_\n\n```php\n$strategy-\u003eforever('foo', 'FOO');\n\n// or\n\n$strategy-\u003eset('foo', 'FOO', 0);\n```\n\n## foreverMultiple\n\nPersists forever a set of `key =\u003e value` pairs in the cache.\n\n```php\nforeverMultiple(array $values): $this\n```\n\n`$values` - A list of `key =\u003e value` pairs for a multiple-set operation.\n\n_Example:_\n\n```php\n$strategy-\u003eforeverMultiple(['foo' =\u003e 'FOO', 'bar' =\u003e 'BAR']);\n\n// or\n\n$strategy-\u003esetMultiple(['foo' =\u003e 'FOO', 'bar' =\u003e 'BAR'], 0);\n```\n\n## delete\n\nDelete an item from the cache by its unique key.\n\n```php\ndelete(string $key): $this\n```\n\n`$key` - The unique cache key of the item to delete.\n\n_Example:_\n\n```php\n$strategy-\u003edelete('foo');\n```\n\n## deleteMultiple\n\nDelete multiple items from the cache by their unique keys.\n\n```php\ndeleteMultiple(array $keys): $this\n```\n\n`$keys` - The unique cache keys of the items to delete.\n\n_Example:_\n\n```php\n$strategy-\u003edeleteMultiple(['foo', 'bar']);\n```\n\n## clear\n\nWipes clean the entire cache's keys.\n\n```php\nclear(): $this\n```\n\n_Example:_\n\n```php\n$strategy-\u003eclear();\n```\n\n## remember\n\nSometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist.\n\n```php\nremember(string $key, callable($this): mixed $callable, ?int $ttl = null): mixed\n```\n\n`$key` - The unique key of this item in the cache;  \n`$callable` - The value callable of the item to store when the key is not present in the cache. The value must be serializable;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.\n\n_Example:_\n\n```php\n$strategy-\u003eremember('foo', function() {\n    return 'FOO';\n});\n```\n\n## increment\n\nIncrement a value.\n\n```php\nincrement(string $key, int $amount = 1, ?int $ttl = null): $this\n```\n\n`$key` - The unique key of this item in the cache;  \n`$abount` - The amount to increment;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.\n\n_Example:_\n\n```php\n$strategy-\u003eincrement('foo');\n\n$strategy-\u003eincrement('foo', 10);\n```\n\n## decrement\n\nDecrement a value.\n\n```php\ndecrement(string $key, int $amount = 1, ?int $ttl = null): $this\n```\n\n`$key` - The unique key of this item in the cache;  \n`$abount` - The amount to decrement;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.\n\n_Example:_\n\n```php\n$strategy-\u003edecrement('foo');\n\n$strategy-\u003edecrement('foo', 10);\n```\n\n## incrementFloat\n\nIncrement a float value.\n\n```php\nincrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this\n```\n\n`$key` - The unique key of this item in the cache;  \n`$abount` - The amount to increment;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.\n\n_Example:_\n\n```php\n$strategy-\u003eincrementFloat('foo');\n\n$strategy-\u003eincrementFloat('foo', 1.5);\n```\n\n## decrementFloat\n\nDecrement a float value.\n\n```php\ndecrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this\n```\n\n`$key` - The unique key of this item in the cache;  \n`$abount` - The amount to decrement;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.\n\n_Example:_\n\n```php\n$strategy-\u003edecrementFloat('foo');\n\n$strategy-\u003edecrementFloat('foo', 1.5);\n```\n\n## touch\n\nSet a new expiration on an item.\n\n```php\ntouch(string $key, ?int $ttl = null): $this\n```\n\n`$key` - The unique key of this item in the cache;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.\n\n_Example:_\n\n```php\n$strategy-\u003etouch('foo', 100);\n```\n\n## pull\n\nRetrieve and delete an item from the cache.\n\n```php\npull(string $key, $default = null): mixed\n```\n\n`$key` - The unique key of this item in the cache;  \n`$default` - Default value to return for keys that do not exist.\n\nReturn the value of the item from the cache, or `$default` in case of cache miss.\n\n_Example:_\n\n```php\n$strategy-\u003epull('foo'); // return foo value\n\n$strategy-\u003epull('foo'); // return null\n```\n\n## add\n\nPersists data in the cache if it's not present.\n\n```php\nadd(string $key, $value, ?int $ttl = null): $this\n```\n\n`$key` - The key of the item to store;  \n`$value` - The value of the item to store, must be serializable;  \n`$ttl` - Optional. The TTL value of this item. If no value is sent and\n            the driver supports TTL then the library may set a default value\n            for it or let the driver take care of that.  \n\nReturn `true` if the item is actually added to the cache. Otherwise, return `false`.\n\n_Example:_\n\n```php\n$strategy-\u003eadd('foo', 'FOO'); // return true\n\n$strategy-\u003eadd('foo', 'FOO2'); // return false\n```\n\n# License\n\nMIT © [Grigorii Duca](http://greg.md)\n\n# Huuuge Quote\n\n![I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. \u0026copy; #horrorsquad](http://greg.md/huuuge-quote-fb.jpg)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-md%2Fphp-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreg-md%2Fphp-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreg-md%2Fphp-cache/lists"}