Skip to content

Commit

Permalink
Added more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Dec 4, 2023
1 parent deec7d0 commit 495b435
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 14 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ of engines available in this library that is PSR-16 compliant:

{:.table}

| Class | Description |
|:----------------------------------------|:--------------------------------------------------------------------|
| \ByJG\Cache\Psr16\NoCacheEngine | Do nothing. Use it for disable the cache without change your code |
| \ByJG\Cache\Psr16\ArrayCacheEngine | Local cache only using array. It does not persists between requests |
| \ByJG\Cache\Psr16\FileSystemCacheEngine | Save the cache result in the local file system |
| \ByJG\Cache\Psr16\MemcachedEngine | Uses the Memcached as the cache engine |
| \ByJG\Cache\Psr16\SessionCachedEngine | uses the PHP session as cache |
| \ByJG\Cache\Psr16\ShmopCachedEngine | uses the shared memory area for cache |
| Class | Description |
|:---------------------------------------------------------------------------------|:--------------------------------------------------------------------|
| [\ByJG\Cache\Psr16\NoCacheEngine](docs/class-no-cache-engine.md) | Do nothing. Use it for disable the cache without change your code |
| [\ByJG\Cache\Psr16\ArrayCacheEngine](docs/class-array-cache-engine.md) | Local cache only using array. It does not persists between requests |
| [\ByJG\Cache\Psr16\FileSystemCacheEngine](docs/class-filesystem-cache-engine.md) | Save the cache result in the local file system |
| [\ByJG\Cache\Psr16\MemcachedEngine](docs/class-memcached-engine.md) | Uses the Memcached as the cache engine |
| [\ByJG\Cache\Psr16\RedisCachedEngine](docs/class-redis-cache-engine.md) | uses the Redis as cache |
| [\ByJG\Cache\Psr16\SessionCachedEngine](docs/class-session-cache-engine.md) | uses the PHP session as cache |
| [\ByJG\Cache\Psr16\ShmopCachedEngine](docs/class-shmop-cache-engine.md) | uses the shared memory area for cache |

To create a new Cache Instance just create the proper cache engine and use it:

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
},
"require": {
"php": ">=5.6.0",
"php": ">=7.0",
"psr/cache": "^1.0",
"psr/log": "^1.1",
"psr/simple-cache": "^1.0"
Expand Down
27 changes: 27 additions & 0 deletions docs/class-array-cache-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Class ArrayCacheEngine

This class is a simple cache engine that uses an array to store the values.
It does not persists between requests.

It is ideal to use on unit tests or when you need a simple cache engine.


## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\ArrayCacheEngine()
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createArrayPool()
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\ArrayCacheEngine());
```


30 changes: 30 additions & 0 deletions docs/class-filesystem-cache-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Class FilesystemCacheEngine

This class uses the Filesystem as the cache engine.

## Defining the Path

The FileSystemCacheEngine expects a prefix and a path to store the cache files.
The prefix is used to avoid collision between different applications using the same cache path.
If the path is not defined, the default is the system temporary path.


## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\FileSystemCacheEngine($path, $prefix)
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createFilePool($path, $prefix, $bufferSize = 10)
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\FileSystemCacheEngine($path, $prefix));
```


34 changes: 34 additions & 0 deletions docs/class-memcached-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Class MemcachedEngine

This class uses the Memcached as the cache engine.

## Defining the Servers

The constructor expects an array of servers.
Each server is an item in the array with the following format:

```php
$servers = [
'localhost:11211',
]
```

## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\MemcachedEngine($servers)
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createMemcachedPool($servers)
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\MemcachedEngine($servers));
```


24 changes: 24 additions & 0 deletions docs/class-no-cache-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Class NoCacheEngine

This class don't cache. Use it for disable the cache without change your code.


## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\NoCacheEngine();
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createNullPool();
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\NoCacheEngine());
```


31 changes: 31 additions & 0 deletions docs/class-redis-cache-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Class RedisCacheEngine

This class uses the Redis as the cache engine.

## Defining the Servers

The constructor expects a string with the server and port.

```php
$server = 'localhost:5678'
```

## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\RedisCacheEngine($server, $password)
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createRedisCacheEngine($server, $password)
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\RedisCacheEngine($server, $password));
```


27 changes: 27 additions & 0 deletions docs/class-session-cache-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Class SessionCacheEngine

This class uses the PHP Session as the cache engine.
This will persist the cache between requests while the user session is active.

The cache is not shared between different users.


## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\SessionCacheEngine($prefix)
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createSessionPool($prefix, $bufferSize = 10)
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\SessionCacheEngine($prefix));
```


38 changes: 38 additions & 0 deletions docs/class-shmop-cache-engine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Class ShmopCacheEngine

This class uses the PHP Shmop as the cache engine.

The Shared memory allows multiple processes to access the same data in memory.
You can use it to share data among running PHP scripts in the same server.

## Configuration

These are the default values for the configuration:

```php
$config = [
'max-size' => 524288, // 512Kb
'default-permission' = > '0700',
];
```


## PSR-16 Constructor

```php
$cache = new \ByJG\Cache\Psr16\ShmopCacheEngine($config, $prefix)
```

## PSR-6 Constructor

```php
$cachePool = \ByJG\Cache\Factory::createSessionPool($prefix, $bufferSize = 10)
```

or

```php
$cachePool = new \ByJG\Cache\Psr6\CachePool(new \ByJG\Cache\Psr16\ShmopCacheEngine($config, $prefix));
```


4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public static function createSessionPool($prefix = null, $bufferSize = null)
);
}

public static function createFilePool($prefix = null, $bufferSize = null, $logger = null)
public static function createFilePool($prefix = null, $path = null, $bufferSize = null, $logger = null)
{
return new CachePool(
new FileSystemCacheEngine($prefix, $logger),
new FileSystemCacheEngine($prefix, $path, $logger),
$bufferSize
);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Psr16/FileSystemCacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class FileSystemCacheEngine extends BaseCacheEngine implements CacheLockInterfac
protected $logger = null;

protected $prefix = null;
protected $path = null;

public function __construct($prefix = 'cache', $logger = null)
public function __construct($prefix = 'cache', $path = null, $logger = null)
{
$this->prefix = $prefix;
$this->path = $path ?? sys_get_temp_dir();

$this->logger = $logger;
if (is_null($logger)) {
Expand Down Expand Up @@ -146,7 +148,7 @@ public function lock($key)
*/
public function unlock($key)
{

$this->logger->info("[Filesystem cache] Unlock '$key'");

$lockFile = $this->fixKey($key) . ".lock";
Expand All @@ -163,7 +165,7 @@ public function isAvailable()

protected function fixKey($key)
{
return sys_get_temp_dir() . '/'
return $this->path . '/'
. $this->prefix
. '-' . preg_replace("/[\/\\\]/", "#", $key)
. '.cache';
Expand Down

0 comments on commit 495b435

Please sign in to comment.