{"id":21057795,"url":"https://github.com/driftphp/reactphp-key-value","last_synced_at":"2025-05-15T23:33:58.877Z","repository":{"id":56972190,"uuid":"357332831","full_name":"driftphp/reactphp-key-value","owner":"driftphp","description":"Key-value inmemory cache on top of ReactPHP","archived":false,"fork":false,"pushed_at":"2021-04-29T15:37:05.000Z","size":13,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-19T14:19:57.725Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/driftphp.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":"2021-04-12T20:39:24.000Z","updated_at":"2022-12-31T17:30:59.000Z","dependencies_parsed_at":"2022-08-21T10:20:13.567Z","dependency_job_id":null,"html_url":"https://github.com/driftphp/reactphp-key-value","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-key-value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-key-value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-key-value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/driftphp%2Freactphp-key-value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/driftphp","download_url":"https://codeload.github.com/driftphp/reactphp-key-value/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254442429,"owners_count":22071864,"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":[],"created_at":"2024-11-19T17:04:45.643Z","updated_at":"2025-05-15T23:33:58.826Z","avatar_url":"https://github.com/driftphp.png","language":"PHP","readme":"# ReactPHP key-value cache\n\n[![CircleCI](https://circleci.com/gh/driftphp/reactphp-cache.svg?style=svg)](https://circleci.com/gh/driftphp/reactphp-cache)\n\nJust a simple key-value local cache for your [ReactPHP](https://reactphp.org/)\nprojects\n\n## Set a key\n\nYou can set a value given a key in this cache. As simple as it sounds.\n\n```php\nuse React\\EventLoop\\Factory;\nuse Drift\\Cache\\LocalKeyValueCache;\n\n$loop = Factory::create();\n$cache = new LocalKeyValueCache($loop);\n$cache-\u003eset('my_key', 'Any value');\n```\n\n### Define TTL\n\nYou can add a **TTL** for this key. After *n* seconds, this key will be\nautomatically deleted.\n\n```php\nuse React\\EventLoop\\Factory;\nuse Drift\\Cache\\LocalKeyValueCache;\n\n$loop = Factory::create();\n$ttl = 0.1; // Means 0.1 second (100 milliseconds)\n$cache = new LocalKeyValueCache($loop);\n$cache-\u003eset('my_key', 'Any value', $ttl);\n```\n\n## Get a key\n\nYou can get a value from this cache by using the key. If the value is present\ninside the cache, this one will be returned with no transformations. Otherwise,\nnull will be returned.\n\n```php\nuse React\\EventLoop\\Factory;\nuse Drift\\Cache\\LocalKeyValueCache;\n\n$loop = Factory::create();\n$cache = new LocalKeyValueCache($loop);\n$cache-\u003eset('my_key', 'Any value');\n$value = $cache-\u003eget('my_key');\n```\n\n### Refresh TTL on access\n\nTTL can offer a proper way to basically clean elements that are almost not\nused. By defining a value in TTL, by default this key will be deleted after\n*n* seconds, no matter how many times this key has been requested until this\nmoment. If we want to automatically refresh this TTL each time we access to a\nkey, we can use this feature.\n\nIn this example, we can see that the key is defined with a **TTL** of 2 seconds\nand is requested each second, enabling the `refreshTTL` flag. In normal \ncircumstances, after 2 seconds the `get` method should return null, but because\nwe are forcing the cache to refresh this TTL when we access the key, as long as\nwe don't have time gaps larger than 2 seconds, our key will always be available.\n\n```php\nuse React\\EventLoop\\Factory;\nuse Drift\\Cache\\LocalKeyValueCache;\n\n$loop = Factory::create();\n$cache = new LocalKeyValueCache($loop);\n$ttl = 2; // Means 2 seconds\n$cache-\u003eset('my_key', 'Any value');\n\n// ... After 1 second\n$value = $cache-\u003eget('my_key', true); // Found\n\n// ... After 1 second\n$value = $cache-\u003eget('my_key', true); // Found\n\n// ... After 1 second\n$value = $cache-\u003eget('my_key', true); // Found\n\n// ... After 3 second\n$value = $cache-\u003eget('my_key', true); // Not Found\n```\n\nWith this strategy you will only save locally these values used most frequent,\nfinding this way a nice equilibrium between cache efficiency and storage size.\n\n## Delete a key\n\nYou can manually delete a key. If the key is not found inside the cache, nothing\nwill happen.\n\n```php\nuse React\\EventLoop\\Factory;\nuse Drift\\Cache\\LocalKeyValueCache;\n\n$loop = Factory::create();\n$cache = new LocalKeyValueCache($loop);\n$cache-\u003edelete('my_key');\n```\n\n## Using the middleware\n\nIn your applications you might want to use this cache as a simple and thin\nmiddleware layer, so you can easily enable and disable without changing your\ndomain implementation.\n\nWell, then you should use the `KeyValueCacheMiddleware` class, acting as an\nuncoupled piece in the middle.\n\nSo, having this original code in PHP\n\n```php\nreturn $this\n    -\u003edbConnection\n    -\u003efind('token', '123');\n```\n\nYou could easily add a simple layer that caches during 10 minutes, updating the\nkey freshness each time this one es requested.\n\n```php\nuse React\\EventLoop\\Factory;\nuse Drift\\Cache\\LocalKeyValueCache;\nuse Drift\\Cache\\KeyValueCacheMiddleware;\n\n$loop = Factory::create();\n$ttl = 600; // 10 minutes\n$cache = new LocalKeyValueCache($loop);\n$middleware = new KeyValueCacheMiddleware($cache);\n\nreturn $middleware-\u003egetOrAsk('token_123', function() {\n    return $this\n        -\u003edbConnection\n        -\u003efind('token', '123');\n}, $ttl, true);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdriftphp%2Freactphp-key-value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdriftphp%2Freactphp-key-value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdriftphp%2Freactphp-key-value/lists"}