PHP Classes

How to Implement a PHP Cache Control for Class Functions Using the Trait Mnemosyne Attributes based PHP caching : Cache class functions' output using annotations

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-12-13 (14 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 9 This week: 9All time: 11,475 This week: 5Up
Version License PHP version Categories
mnemosyne 1.0.1GNU General Publi...8Cache, Language, PHP 8
Description 

Author

This package can cache class functions' output using annotations.

It provides a trait that can be used in classes with functions that generate output to be cached.

The trait can read the class annotation comments to extract parameter values used in caching the functions' output.

The stored cache is invalidated when the parameter values extracted from the class comments change.

The trait also provides a function to invalidate the stored cache values.

Currently, it supports extracting cache parameter values from comments that define:

- The function parameters that are used to define the cache key

- The time to live for the stored cached values

- The function parameters that are checked to determine if the stored cache values are still valid

Picture of Carlos Artur Curvelo da Matos
  Performance   Level  
Name: Carlos Artur Curvelo da ... <contact>
Classes: 25 packages by
Country: Portugal Portugal
Innovation award
Innovation award
Nominee: 16x

Winner: 2x

Documentation

Mnemosyne - PHP Attribute-based Caching Library

PHP Lint PHPUnit Tests PHP Composer

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.

Features

  • Attribute-based caching configuration
  • Automatic cache key generation
  • Parameter-based cache keys with interpolation
  • Automatic and manual cache invalidation
  • Cache tags for group invalidation
  • PSR-16 (SimpleCache) compatibility
  • Flexible cache key templates
  • Smart serialization handling: - Automatic serialization of complex objects - Optional raw storage for simple data types - Full control over serialization behavior

Installation

composer require cmatosbc/mnemosyne

Usage

To use Mnemosyne in your classes, you must: 1. Use the CacheTrait in your class 2. Inject a PSR-16 compatible cache implementation 3. Apply the Cache attribute to methods you want to cache

Basic Usage

use Mnemosyne\Cache;
use Mnemosyne\CacheTrait;
use Psr\SimpleCache\CacheInterface;

class UserService
{
    use CacheTrait;  // Required to enable caching functionality

    public function __construct(CacheInterface $cache)
    {
        $this->cache = $cache;
    }

    #[Cache(ttl: 3600)]
    public function getUser(int $id): array
    {
        return $this->cacheCall('doGetUser', func_get_args());
    }

    private function doGetUser(int $id): array
    {
        // Expensive database query here
        return ['id' => $id, 'name' => 'John Doe'];
    }
}

Serialization Control

The Cache attribute allows you to control how values are stored in cache:

class UserService
{
    use CacheTrait;

    // Automatically serialize complex objects
    #[Cache(key: 'user:{id}', ttl: 3600, serialize: true)]
    public function getUser(int $id): User
    {
        return $this->cacheCall('doGetUser', func_get_args());
    }

    // Store simple arrays without serialization
    #[Cache(key: 'users:list', ttl: 3600, serialize: false)]
    public function getUsersList(): array
    {
        return $this->cacheCall('doGetUsersList', func_get_args());
    }
}

Custom Cache Keys

class UserService
{
    use CacheTrait;

    #[Cache(key: 'user:{id}', ttl: 3600)]
    public function getUser(int $id): array
    {
        return $this->cacheCall('doGetUser', func_get_args());
    }

    #[Cache(key: 'users:dept:{deptId}:status:{status}', ttl: 3600)]
    public function getUsersByDepartment(int $deptId, string $status): array
    {
        return $this->cacheCall('doGetUsersByDepartment', func_get_args());
    }
}

Cache Invalidation

Automatic Invalidation

class UserService
{
    use CacheTrait;

    #[Cache(
        key: 'user:{id}',
        ttl: 3600
    )]
    public function getUser(int $id): array
    {
        return $this->cacheCall('doGetUser', func_get_args());
    }

    #[Cache(invalidates: ['user:{id}'])]
    public function updateUser(int $id, array $data): void
    {
        $this->cacheCall('doUpdateUser', func_get_args());
    }

    #[Cache(
        key: 'user:profile:{id}',
        ttl: 3600,
        invalidates: ['user:{id}', 'users:dept:{deptId}:status:active']
    )]
    public function updateProfile(int $id, int $deptId): array
    {
        return $this->cacheCall('doUpdateProfile', func_get_args());
    }
}

Manual Invalidation

class UserService
{
    use CacheTrait;

    public function forceRefresh(int $userId): void
    {
        $this->invalidateCache("user:$userId");
        // Or invalidate multiple keys:
        $this->invalidateCacheKeys([
            "user:$userId",
            "user:profile:$userId"
        ]);
    }
}

Cache Tags

Cache tags allow you to group related cache entries and invalidate them together. This is useful for managing cache dependencies and bulk invalidation.

class UserService
{
    use CacheTrait;

    #[Cache(
        key: 'user:{id}',
        ttl: 3600,
        tags: ['user', 'user-{id}']
    )]
    public function getUser(int $id): array
    {
        return $this->cacheCall('doGetUser', func_get_args());
    }

    #[Cache(
        key: 'user:profile:{id}',
        ttl: 3600,
        tags: ['user', 'user-{id}']
    )]
    public function getUserProfile(int $id): array
    {
        return $this->cacheCall('doGetUserProfile', func_get_args());
    }

    public function updateUser(int $id): void
    {
        // Invalidate all caches for a specific user
        $this->invalidateTag("user-$id");
    }

    public function clearAllUserCaches(): void
    {
        // Invalidate all user-related caches
        $this->invalidateTag('user');
    }
}

Tags 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.

Best Practices

  1. Split cached methods into two parts: - A public method with the Cache attribute that handles caching - A private method with the actual implementation
  2. Use meaningful cache keys that reflect the data structure
  3. Set appropriate TTL values based on data volatility
  4. Use cache invalidation when data is modified
  5. Consider using cache tags for group invalidation

Testing

The library includes comprehensive PHPUnit tests. Run them with:

./vendor/bin/phpunit

License

This 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.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


  Files folder image Files (17)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (3 files)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file .phpcs-cache Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (17)  /  .github  
File Role Description
Files folder imageworkflows (3 files)

  Files folder image Files (17)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file composer.yml Data Auxiliary data
  Accessible without login Plain text file lint.yml Data Auxiliary data
  Accessible without login Plain text file phpunit.yml Data Auxiliary data

  Files folder image Files (17)  /  src  
File Role Description
  Plain text file Cache.php Class Class source
  Plain text file CacheAttribute.php Class Class source
  Plain text file CacheTrait.php Class Class source

  Files folder image Files (17)  /  tests  
File Role Description
Files folder imageFixtures (2 files)
  Plain text file CacheAttributeTest.php Class Class source
  Plain text file CacheTagsTest.php Class Class source

  Files folder image Files (17)  /  tests  /  Fixtures  
File Role Description
  Plain text file MockCache.php Class Class source
  Plain text file TestService.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:9
This week:9
All time:11,475
This week:5Up