PHP Classes

PHP PSR Cache: Cache data in files with a PSR compliant interface

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-03-19 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 23 All time: 11,168 This week: 69Up
Version License PHP version Categories
psr-cache 1.0MIT/X Consortium ...5Files and Folders, Cache, PHP 7, PSR
Description 

Authors

Ujah Chigozie
Nanoblock Technology


Contributor

This package can cache data in files with a PSR-compliant interface.

It provides wrapper classes around the Luminova framework file cache package to provide a data caching support.

This package provides an interface compliant with PHP Standard Recommendation 6 specification for caching data.

Picture of Ujah Chigozie peter
  Performance   Level  
Name: Ujah Chigozie peter <contact>
Classes: 24 packages by
Country: Nigeria Nigeria
Age: 33
All time rank: 202510 in Nigeria Nigeria
Week rank: 33 Up2 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 11x

Documentation

PSR CACHE

PSR Cache for Luminova Framework CachePool, SimpleCache. To use this library you need to install Luminova Framework first.

Installation

Via Composer

composer require nanoblocktech/psr-cache

Usage

use \Luminova\Psr\Cache\CachePool;
use \Luminova\Psr\Cache\CacheItem;

$pool = new CachePool('my_cache', 'my_cache_folder_name');

// Set a cache item
$item = $pool->getItem('cache_key');
$item->set('This is my cache data');
$pool->save($item);

// Get a cache item
$data = $pool->getItem('cache_key')->get();

// Check if the cache item exists
if (!$item->isHit()) {
    $data = databaseLoadData();
    $item->expiresAfter(new DateInterval('PT1H')); 
    $item->set($data);
    $pool->save($item);
} else {
    // Data exists in the cache, use it directly
    $data = $item->get();
}

CachePool Methods

Initialize the class with storage location name and folder subfolder name.

$pool = new CachePool(string $storage = 'psr_cache_storage', string $folder = 'psr');

// Retrieves an item from the cache.
$pool->getItem('cache_key'): CacheItem;

// Retrieves multiple cache items at once.
$pool->getItems(array ['key1', 'key2']): iterable<key, CacheItem>;

// Determines whether an item exists in the cache.
$pool->hasItem(string 'cache_key'): bool;

// Persists a cache item immediately.
$pool->save(CacheItemInterface $item): bool;

// Save a deferred cache item.
$pool->saveDeferred(CacheItemInterface $item): bool;

// Commits any deferred cache items.
$pool->commit(): bool;

// Rollback If any deferred commit failed to save, if you prefer not to recommit
$pool->rollback(): bool;

// Deletes an item from the cache.
$pool->deleteItem(string 'cache_key'): bool;

// Deletes multiple items from the cache.
$pool->deleteItems(array ['key1', 'key2']): bool;

// Clear all cached entries 
$pool->clear(): bool;

use \Luminova\Psr\Cache\SimpleCache;

$simple = new SimpleCache(string 'my_cache', string 'my_cache_folder_name');

// Set a cache item
$data = $simple->get('cache_key', 'NO_DATA');
if($item === 'NO_DATA'){
    $data = 'This is my cache data';
    $simple->set('cache_key', $data);
}

SimpleCache Methods

Initialize the class with storage location name and folder subfolder name.

$simple = new ?SimpleCache?(string $storage = 'psr_cache_storage', string $folder = 'psr');

// Retrieves an item from the cache.
$simple->get(string 'cache_key', mixed 'default value'): mixed;

// Retrieves multiple cache items at once.
$simple->getMultiple(array ['key1', 'key2'], 'default on empty key value'): iterable<key, mixed>;

// Determines whether an item exists in the cache.
$simple->has(string 'cache_key'): bool;

// Persists a cache item immediately with an optional TTL.
$simple->set(string 'cache_key', mixed 'data to save', null|int|DateInterval 60): bool;

// Persists a set of key => value pairs in the cache, with an optional TTL.
$simple->setMultiple(array ['key1' => 'data 1', 'key2' => 'data 2'], null|int|DateInterval 60): bool;

// Deletes an item from the cache.
$simple->delete(string 'cache_key'): bool;

// Deletes multiple items from the cache.
$simple->deleteMultiple(array ['key1', 'key2']): bool;

// Clears all cache entries.
$simple->clear(): bool;

CacheItem Methods

Initialize the class with key, content to save and specify the hit state optionally.

$item = new CacheItem(string $key, mixed $content = null, ?bool $isHit = null);

//Retrieves the key of the cache item.
$item->getKey(): string;

//Retrieves the value of the cache item.
$item->get(): mixed;

//Check if the cache item is a hit.
$item->isHit(): bool;

//Sets the value of the cache item.
$item->set(mixed $value): static;

//Sets the expiration time of the cache item.
$item->expiresAt(?DateTimeInterface $expiration): static;

//Sets the expiration time of the cache item relative to the current time.
$item->expiresAfter(int|DateInterval|null $time): static;

  Files folder image Files  
File Role Description
Files folder imagesrc (3 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageExceptions (2 files)
Files folder imageHelper (1 file)
  Plain text file CacheItem.php Class Class source
  Plain text file CachePool.php Class Class source
  Plain text file SimpleCache.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file CacheException.php Class Class source
  Plain text file InvalidArgumentException.php Class Class source

  Files folder image Files  /  src  /  Helper  
File Role Description
  Plain text file Helper.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:23
This week:0
All time:11,168
This week:69Up