{"id":19112903,"url":"https://github.com/chipslays/cache","last_synced_at":"2025-02-22T11:40:25.746Z","repository":{"id":210004963,"uuid":"725470062","full_name":"chipslays/cache","owner":"chipslays","description":"🗄️ A simple and primitive library for caching values for PHP \u003e8.1.","archived":false,"fork":false,"pushed_at":"2024-04-16T12:12:41.000Z","size":698,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-03T04:52:31.997Z","etag":null,"topics":["cache","php-cache"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chipslays.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-30T07:58:40.000Z","updated_at":"2023-11-30T11:45:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"655adc1d-608e-47a8-839d-b1f9ba0272e9","html_url":"https://github.com/chipslays/cache","commit_stats":null,"previous_names":["chipslays/cache"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chipslays","download_url":"https://codeload.github.com/chipslays/cache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240170056,"owners_count":19759141,"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","php-cache"],"created_at":"2024-11-09T04:34:28.928Z","updated_at":"2025-02-22T11:40:25.725Z","avatar_url":"https://github.com/chipslays.png","language":"PHP","readme":"# 🗄️ Cache\n\nA simple and primitive library for caching values for PHP \u003e8.1.\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"/.github/static/hero.png\"\u003e\n\u003c/p\u003e\n\n## Installation\n\n```bash\ncomposer require please/cache\n```\n\n## Examples\n\nYou can find usage examples [here](/examples).\n\n## Drivers\n\nAvailable drivers:\n- [Filesystem](#filesystem)\n  - This driver uses the file system to store the cache.\n- [Memory](#memory)\n  - This driver uses the memory to store the cache.\n- [Session](#session)\n  - This driver uses the `$_SESSION` to store the cache.\n\n### Filesystem\n\nThis driver uses the file system to store the cache.\n\n```php\nuse Please\\Cache\\Cache;\nuse Please\\Cache\\Drivers\\Filesystem;\n\n$cache = new Cache(new Filesystem);\n\n$cache-\u003eset('foo', 'bar');\n$cache-\u003eget('foo'); // bar\n```\n\nYou can provide a specific parameters.\n\n```php\nuse Please\\Cache\\Cache;\nuse Please\\Cache\\Drivers\\Filesystem;\n\n$driver = new Filesystem(folder: '/path/to/folder', prefix: 'data');\n\n$cache = new Cache($driver);\n\n$cache-\u003eset('foo', fn () =\u003e ['bar']);\n$cache-\u003eget('foo'); // ['bar']\n```\n\n### Memory\n\nThis driver uses the memory to store the cache.\n\n\u003e [!WARNING]\n\u003e After the script completes the memory will be cleared.\n\n```php\nuse Please\\Cache\\Cache;\nuse Please\\Cache\\Drivers\\Memory;\n\n$cache = new Cache(new Memory);\n$anotherCacheInstance = new Cache(new Memory);\n```\n\nBy default, cache created with Memory driver under hood.\n\n```php\nuse Please\\Cache\\Cache;\n\n$cache = new Cache;\n```\n\n### Session\n\nThis driver uses the `$_SESSION` to store the cache.\n\n```php\nuse Please\\Cache\\Cache;\nuse Please\\Cache\\Drivers\\Session;\n\n$cache = new Cache(new Session);\n```\n\nYou can pass the key in which the cache will be stored.\n\n```php\nuse Please\\Cache\\Cache;\nuse Please\\Cache\\Drivers\\Session;\n\n$apiCache = new Cache(new Session('_api'));\n$imageCache = new Cache(new Session('_images'));\n```\n\n## Cache\n\nYou can create as many Cache instances as you need.\n\n```php\nuse Please\\Cache\\Cache;\nuse Please\\Cache\\Drivers\\Session;\nuse Please\\Cache\\Drivers\\Filesystem;\n\n$videoCache = new Cache(new Session('your unique key'));\n$imageCache = new Cache(new Filesystem('/path/to/images'));\n```\n\nBy default, for serialization uses native PHP functions `serialize()` and `unserialize()`.\n\nYou can create and pass your own serializer if you need to, for example to serialize closures, classes, etc.\n\n```php\nuse Please\\Cache\\Cache;\nuse Please\\Cache\\Drivers\\Filesystem;\nuse Please\\Cache\\Serializers\\Contracts\\Serializer;\n\nclass JsonSerializer implements Serializer\n{\n    public function serialize(mixed $value): string\n    {\n        return json_encode($value);\n    }\n\n    public function unserialize(mixed $value): mixed\n    {\n        return json_decode($value, true);\n    }\n}\n\n$cache = new Cache(new Filesystem, new JsonSerializer);\n```\n\n## Methods\n\n### set()\n\nPersists value in the cache, uniquely referenced by a key with an optional expiration TTL time.\n\nParameter | Required | Default\n--- | --- | ---\n`string $key` | `true` |\n`mixed $value` | `true` |\n`int\\|string $ttl` | `false` | `1 year`\n\n```php\n$cache-\u003eset(key: 'foo', value: 'bar', ttl: 3600);\n```\n\nYou can pass the TTL value as a string like for the `strtotime() ` function.\n\n```php\n$cache-\u003eset('foo', ['bar', 'baz'], '1 day');\n\n// the example above is equivalent to this code\n$ttl = strtotime('1 day') - time();\n$cache-\u003eset('foo', 'bar', $ttl);\n```\n\n### get()\n\nFetches a value from the cache.\n\nParameter | Required | Default\n--- | --- | ---\n`string $key` | `true` |\n`mixed $default ` | `false` | `null`\n\n```php\n$cache-\u003eget(key: 'foo', default: 'baz');\n```\n\nPass a default value as a `Closure`, it will be executed lazily if the key is not found.\n\n```php\n$cache-\u003eget('foo', fn () =\u003e 'baz');\n```\n\n### has()\n\nDetermines whether an item is present in the cache.\n\nParameter | Required | Default\n--- | --- | ---\n`string $key` | `true` |\n\n```php\n$cache-\u003eset('foo', 'bar');\n\n$cache-\u003ehas('foo'); // true\n$cache-\u003ehas('baz'); // false\n```\n\n### clear()\n\nWipes all cache.\n\n\u003e [!NOTE]\n\u003e The `$cacheInstance-\u003eclear()` method will only work for the instance in which it was called.\n\n```php\n$cache-\u003eset('foo1', 'bar1')-\u003ehas('foo1'); // true\n$cache-\u003eset('foo2', 'bar2')-\u003ehas('foo2'); // true\n\n$cache-\u003eclear();\n\n$cache-\u003ehas('foo1'); // false\n$cache-\u003ehas('foo2'); // false\n```\n\n### forget()\n\nDelete cache by key.\n\nParameter | Required | Default\n--- | --- | ---\n`string $key` | `true` |\n\n```php\n$cache-\u003eset('foo', 'bar')-\u003ehas('foo'); // true\n\n$cache-\u003eforget('foo');\n\n$cache-\u003ehas('foo'); // false\n```\n\n### pluck()\n\nRemoves and returns an item from the cache by its key.\n\nParameter | Required | Default\n--- | --- | ---\n`string $key` | `true` |\n`mixed $default` | `false` | `null`\n\n```php\n$cache-\u003eset('foo', 'bar')-\u003ehas('foo'); // true\n\n$cache-\u003epluck('foo'); // bar\n\n$cache-\u003ehas('foo'); // false\n```\n\n### through()\n\nIf the closure is not cached, then executes it, otherwise returns the cached result of closure execution.\n\nThis method used [`ClosureHash`](/src/Support/ClosureHash.php) under hood.\n\n\u003e [!NOTE]\n\u003e The closure must return a value suitable for serialization by the [serializer](/src/Serializers/) you choose.\n\nParameter | Required | Default\n--- | --- | ---\n`string $callback` | `true` |\n`int\\|string $ttl` | `false` | `1 year`\n\n```php\n$closure = function () {\n    return mt_rand();\n};\n\n$cache-\u003ethrough($closure);\n$cache-\u003ethrough($closure); // returns cached result of closure execution\n```\n\n## License\nOpen-sourced software licensed under the [MIT license](https://opensource.org/license/mit/).","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchipslays%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchipslays%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchipslays%2Fcache/lists"}