Skip to content

Commit 1083ed7

Browse files
authored
Update README.md
1 parent 33a5f80 commit 1083ed7

File tree

1 file changed

+114
-50
lines changed

1 file changed

+114
-50
lines changed

README.md

+114-50
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
# PSR CACHE
1+
# Luminova PSR Cache Interface
22

3-
PSR Cache for [Luminova Framework](https://github.com/luminovang/luminova/) `CachePool`, `SimpleCache`.
4-
To use this library you need to install Luminova Framework first.
3+
The PSR Cache implementation for the [Luminova Framework](https://luminova.ng), [Luminova Framework GitHub](https://github.com/luminovang/luminova/) providing `CachePool` and `SimpleCache` class.
4+
This library enables the use of both file-based and memory-based (Memcached) caching systems with an easy-to-use API.
55

6+
For more information read the official [documentation](https://luminova.ng/docs/3.3.0/cache/psr).
67

7-
### Installation
8+
---
89

9-
Via Composer
10+
### Installation
1011

11-
```bash
12+
Via Composer:
13+
14+
```bash
1215
composer require nanoblocktech/psr-cache
1316
```
1417

15-
### Usage
18+
---
19+
20+
## Cache Pool Class
21+
22+
The `CachePool` class provides an interface to manage cache items.
23+
It supports multiple cache storage driver, such as file-based or memory-based (Memcached) caching.
24+
25+
---
26+
27+
### Usage Example
28+
1629
```php
30+
<?php
1731
use \Luminova\Psr\Cache\CachePool;
1832
use \Luminova\Psr\Cache\CacheItem;
1933

20-
$pool = new CachePool('my_cache', 'my_cache_folder_name');
34+
$pool = CachePool::withFileCache('my_cache', 'my_cache_folder');
2135

2236
// Set a cache item
2337
$item = $pool->getItem('cache_key');
@@ -39,49 +53,76 @@ if (!$item->isHit()) {
3953
}
4054
```
4155

42-
### CachePool Methods
56+
---
57+
58+
### CachePool Class Methods
4359

44-
Initialize the class with `storage` location name and `folder` subfolder name.
45-
```php
46-
$pool = new CachePool(string $storage = 'psr_cache_storage', string $folder = 'psr');
60+
#### Constructor
61+
62+
```php
63+
$pool = new CachePool(
64+
string|null $storage = 'psr_cache_storage',
65+
string|null $subfolderOrId = 'psr',
66+
string|null $driver = CachePool::FILECACHE
67+
);
4768
```
4869

70+
- **$storage**: Cache storage name to differentiate cache spaces (defaults to `'psr_cache_storage'`).
71+
- **$subfolderOrId**: Optional subfolder for file-based cache or Memcached persistent ID (defaults to `'psr'`).
72+
- **$driver**: Optional cache driver, either `CachePool::FILECACHE` (default) or `CachePool::MEMCACHED`.
73+
74+
---
75+
76+
#### Methods
77+
4978
```php
50-
// Retrieves an item from the cache.
79+
// Retrieve a cache item by key.
5180
$pool->getItem('cache_key'): CacheItem;
5281

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

56-
// Determines whether an item exists in the cache.
57-
$pool->hasItem(string 'cache_key'): bool;
85+
// Check if a cache item exists.
86+
$pool->hasItem('cache_key'): bool;
5887

59-
// Persists a cache item immediately.
88+
// Save a cache item.
6089
$pool->save(CacheItemInterface $item): bool;
6190

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

65-
// Commits any deferred cache items.
94+
// Commit deferred cache items.
6695
$pool->commit(): bool;
6796

68-
// Rollback If any deferred commit failed to save, if you prefer not to recommit
97+
// Rollback deferred cache items.
6998
$pool->rollback(): bool;
7099

71-
// Deletes an item from the cache.
72-
$pool->deleteItem(string 'cache_key'): bool;
100+
// Delete a cache item by key.
101+
$pool->deleteItem('cache_key'): bool;
73102

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

77-
// Clear all cached entries
106+
// Clear all cached entries.
78107
$pool->clear(): bool;
79108
```
80109

110+
---
111+
112+
## Simple Cache Class
113+
114+
The `SimpleCache` class provides a simplified interface for interacting with the cache.
115+
It offers basic operations for storing and retrieving cached data, it supports multiple cache storage driver, such as file-based or memory-based (Memcached) caching.
116+
117+
---
118+
119+
### Usage Example
120+
81121
```php
122+
<?php
82123
use \Luminova\Psr\Cache\SimpleCache;
83124

84-
$simple = new SimpleCache(string 'my_cache', string 'my_cache_folder_name');
125+
$simple = SimpleCache::withFileCache('my_cache', 'my_cache_folder_name');
85126

86127
// Set a cache item
87128
$data = $simple->get('cache_key', 'NO_DATA');
@@ -91,64 +132,87 @@ if($item === 'NO_DATA'){
91132
}
92133
```
93134

94-
### SimpleCache Methods
135+
---
95136

96-
Initialize the class with `storage` location name and `folder` subfolder name.
137+
### SimpleCache Class Methods
138+
139+
#### Constructor
97140

98141
```php
99-
$simple = new ‎SimpleCache‎(string $storage = 'psr_cache_storage', string $folder = 'psr');
142+
$simple = new SimpleCache(
143+
string|null $storage = 'psr_cache_storage',
144+
string|null $subfolderOrId = 'psr',
145+
string|null $driver = SimpleCache::FILECACHE
146+
);
100147
```
101148

149+
---
150+
151+
#### Methods
152+
102153
```php
103-
// Retrieves an item from the cache.
104-
$simple->get(string 'cache_key', mixed 'default value'): mixed;
154+
// Retrieve a cache item by key, with a default fallback value.
155+
$simple->get('cache_key', 'default_value'): mixed;
105156

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

109-
// Determines whether an item exists in the cache.
110-
$simple->has(string 'cache_key'): bool;
160+
// Check if a cache item exists.
161+
$simple->has('cache_key'): bool;
111162

112-
// Persists a cache item immediately with an optional TTL.
113-
$simple->set(string 'cache_key', mixed 'data to save', null|int|DateInterval 60): bool;
163+
// Save a cache item with an optional TTL.
164+
$simple->set('cache_key', 'data_to_save', int|DateInterval|null $ttl = 60): bool;
114165

115-
// Persists a set of key => value pairs in the cache, with an optional TTL.
116-
$simple->setMultiple(array ['key1' => 'data 1', 'key2' => 'data 2'], null|int|DateInterval 60): bool;
166+
// Save multiple cache items with an optional TTL.
167+
$simple->setMultiple(array ['key1' => 'data1', 'key2' => 'data2'], int|DateInterval|null $ttl = 60): bool;
117168

118-
// Deletes an item from the cache.
119-
$simple->delete(string 'cache_key'): bool;
169+
// Delete a cache item.
170+
$simple->delete('cache_key'): bool;
120171

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

124-
// Clears all cache entries.
175+
// Clear all cache entries.
125176
$simple->clear(): bool;
126177
```
127178

128-
### CacheItem Methods
179+
---
129180

130-
Initialize the class with `key`, `content` to save and specify the `hit` state optionally.
181+
## Cache Item Class
182+
183+
The `CacheItem` class represents an individual cache entry.
184+
It includes methods to manipulate and manage the cached data when working with `CachePool` class.
185+
186+
---
187+
188+
### CacheItem Class Methods
189+
190+
#### Constructor
131191

132192
```php
133193
$item = new CacheItem(string $key, mixed $content = null, ?bool $isHit = null);
134194
```
135195

196+
---
197+
198+
#### Methods
199+
136200
```php
137-
//Retrieves the key of the cache item.
201+
// Retrieve the key of the cache item.
138202
$item->getKey(): string;
139203

140-
//Retrieves the value of the cache item.
204+
// Retrieve the value of the cache item.
141205
$item->get(): mixed;
142206

143-
//Check if the cache item is a hit.
207+
// Check if the cache item is a hit.
144208
$item->isHit(): bool;
145209

146-
//Sets the value of the cache item.
210+
// Set the value of the cache item.
147211
$item->set(mixed $value): static;
148212

149-
//Sets the expiration time of the cache item.
213+
// Set the expiration time of the cache item.
150214
$item->expiresAt(?DateTimeInterface $expiration): static;
151215

152-
//Sets the expiration time of the cache item relative to the current time.
216+
// Set the expiration time relative to the current time.
153217
$item->expiresAfter(int|DateInterval|null $time): static;
154218
```

0 commit comments

Comments
 (0)