{"id":23289790,"url":"https://github.com/cmatosbc/mnemosyne","last_synced_at":"2026-02-27T02:31:24.126Z","repository":{"id":267655206,"uuid":"901826199","full_name":"cmatosbc/mnemosyne","owner":"cmatosbc","description":"Mnemosyne is a powerful and flexible caching library for PHP 8.0+ that uses attributes to simplify cache management. It provides automatic caching and invalidation based on method attributes, making it easy to add caching to your application.","archived":false,"fork":false,"pushed_at":"2024-12-16T01:43:18.000Z","size":50,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-11T23:37:45.158Z","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":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cmatosbc.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-11T11:41:58.000Z","updated_at":"2025-01-12T18:34:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"0e65152f-dca6-4222-acc2-c1845a4d16e4","html_url":"https://github.com/cmatosbc/mnemosyne","commit_stats":null,"previous_names":["cmatosbc/mnemosyne"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/cmatosbc/mnemosyne","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmatosbc%2Fmnemosyne","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmatosbc%2Fmnemosyne/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmatosbc%2Fmnemosyne/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmatosbc%2Fmnemosyne/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cmatosbc","download_url":"https://codeload.github.com/cmatosbc/mnemosyne/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmatosbc%2Fmnemosyne/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29883101,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T23:51:21.483Z","status":"online","status_checked_at":"2026-02-27T02:00:06.759Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-12-20T04:18:08.584Z","updated_at":"2026-02-27T02:31:24.086Z","avatar_url":"https://github.com/cmatosbc.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mnemosyne - PHP Attribute-based Caching Library\n\n[![PHP Lint](https://github.com/cmatosbc/mnemosyne/actions/workflows/lint.yml/badge.svg)](https://github.com/cmatosbc/mnemosyne/actions/workflows/lint.yml) [![PHPUnit Tests](https://github.com/cmatosbc/mnemosyne/actions/workflows/phpunit.yml/badge.svg)](https://github.com/cmatosbc/mnemosyne/actions/workflows/phpunit.yml) [![PHP Composer](https://github.com/cmatosbc/mnemosyne/actions/workflows/composer.yml/badge.svg)](https://github.com/cmatosbc/mnemosyne/actions/workflows/composer.yml)\n\nMnemosyne is a powerful and flexible caching library for PHP 8.0+ that uses attributes to simplify cache management. It provides automatic caching and invalidation based on method attributes, making it easy to add caching to your application.\n\n## Features\n\n- Attribute-based caching configuration\n- Automatic cache key generation\n- Parameter-based cache keys with interpolation\n- Automatic and manual cache invalidation\n- Cache tags for group invalidation\n- PSR-16 (SimpleCache) compatibility\n- Flexible cache key templates\n- Smart serialization handling:\n  - Automatic serialization of complex objects\n  - Optional raw storage for simple data types\n  - Full control over serialization behavior\n\n## Installation\n\n```bash\ncomposer require cmatosbc/mnemosyne\n```\n\n## Usage\n\nTo use Mnemosyne in your classes, you must:\n1. Use the `CacheTrait` in your class\n2. Inject a PSR-16 compatible cache implementation\n3. Apply the `Cache` attribute to methods you want to cache\n\n### Basic Usage\n\n```php\nuse Mnemosyne\\Cache;\nuse Mnemosyne\\CacheTrait;\nuse Psr\\SimpleCache\\CacheInterface;\n\nclass UserService\n{\n    use CacheTrait;  // Required to enable caching functionality\n\n    public function __construct(CacheInterface $cache)\n    {\n        $this-\u003ecache = $cache;\n    }\n\n    #[Cache(ttl: 3600)]\n    public function getUser(int $id): array\n    {\n        return $this-\u003ecacheCall('doGetUser', func_get_args());\n    }\n\n    private function doGetUser(int $id): array\n    {\n        // Expensive database query here\n        return ['id' =\u003e $id, 'name' =\u003e 'John Doe'];\n    }\n}\n```\n\n### Serialization Control\n\nThe `Cache` attribute allows you to control how values are stored in cache:\n\n```php\nclass UserService\n{\n    use CacheTrait;\n\n    // Automatically serialize complex objects\n    #[Cache(key: 'user:{id}', ttl: 3600, serialize: true)]\n    public function getUser(int $id): User\n    {\n        return $this-\u003ecacheCall('doGetUser', func_get_args());\n    }\n\n    // Store simple arrays without serialization\n    #[Cache(key: 'users:list', ttl: 3600, serialize: false)]\n    public function getUsersList(): array\n    {\n        return $this-\u003ecacheCall('doGetUsersList', func_get_args());\n    }\n}\n```\n\n### Custom Cache Keys\n\n```php\nclass UserService\n{\n    use CacheTrait;\n\n    #[Cache(key: 'user:{id}', ttl: 3600)]\n    public function getUser(int $id): array\n    {\n        return $this-\u003ecacheCall('doGetUser', func_get_args());\n    }\n\n    #[Cache(key: 'users:dept:{deptId}:status:{status}', ttl: 3600)]\n    public function getUsersByDepartment(int $deptId, string $status): array\n    {\n        return $this-\u003ecacheCall('doGetUsersByDepartment', func_get_args());\n    }\n}\n```\n\n### Cache Invalidation\n\n#### Automatic Invalidation\n\n```php\nclass UserService\n{\n    use CacheTrait;\n\n    #[Cache(\n        key: 'user:{id}',\n        ttl: 3600\n    )]\n    public function getUser(int $id): array\n    {\n        return $this-\u003ecacheCall('doGetUser', func_get_args());\n    }\n\n    #[Cache(invalidates: ['user:{id}'])]\n    public function updateUser(int $id, array $data): void\n    {\n        $this-\u003ecacheCall('doUpdateUser', func_get_args());\n    }\n\n    #[Cache(\n        key: 'user:profile:{id}',\n        ttl: 3600,\n        invalidates: ['user:{id}', 'users:dept:{deptId}:status:active']\n    )]\n    public function updateProfile(int $id, int $deptId): array\n    {\n        return $this-\u003ecacheCall('doUpdateProfile', func_get_args());\n    }\n}\n```\n\n#### Manual Invalidation\n\n```php\nclass UserService\n{\n    use CacheTrait;\n\n    public function forceRefresh(int $userId): void\n    {\n        $this-\u003einvalidateCache(\"user:$userId\");\n        // Or invalidate multiple keys:\n        $this-\u003einvalidateCacheKeys([\n            \"user:$userId\",\n            \"user:profile:$userId\"\n        ]);\n    }\n}\n```\n\n### Cache Tags\n\nCache tags allow you to group related cache entries and invalidate them together. This is useful for managing cache dependencies and bulk invalidation.\n\n```php\nclass UserService\n{\n    use CacheTrait;\n\n    #[Cache(\n        key: 'user:{id}',\n        ttl: 3600,\n        tags: ['user', 'user-{id}']\n    )]\n    public function getUser(int $id): array\n    {\n        return $this-\u003ecacheCall('doGetUser', func_get_args());\n    }\n\n    #[Cache(\n        key: 'user:profile:{id}',\n        ttl: 3600,\n        tags: ['user', 'user-{id}']\n    )]\n    public function getUserProfile(int $id): array\n    {\n        return $this-\u003ecacheCall('doGetUserProfile', func_get_args());\n    }\n\n    public function updateUser(int $id): void\n    {\n        // Invalidate all caches for a specific user\n        $this-\u003einvalidateTag(\"user-$id\");\n    }\n\n    public function clearAllUserCaches(): void\n    {\n        // Invalidate all user-related caches\n        $this-\u003einvalidateTag('user');\n    }\n}\n```\n\nTags support parameter interpolation just like cache keys, allowing you to create dynamic tag names. When a tag is invalidated, all cache entries associated with that tag are automatically removed.\n\n## Best Practices\n\n1. Split cached methods into two parts:\n   - A public method with the Cache attribute that handles caching\n   - A private method with the actual implementation\n   \n2. Use meaningful cache keys that reflect the data structure\n3. Set appropriate TTL values based on data volatility\n4. Use cache invalidation when data is modified\n5. Consider using cache tags for group invalidation\n\n## Testing\n\nThe library includes comprehensive PHPUnit tests. Run them with:\n\n```bash\n./vendor/bin/phpunit\n```\n\n## License\n\nThis project is licensed under the GNU General Public License v3.0 or later - see the LICENSE file for details. This means you are free to use, modify, and distribute this software, but any modifications must also be released under the GPL-3.0-or-later license.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmatosbc%2Fmnemosyne","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcmatosbc%2Fmnemosyne","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmatosbc%2Fmnemosyne/lists"}