Init
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.idea/
|
||||
/vendor
|
||||
composer.lock
|
||||
21
README.md
Normal file
21
README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
## Rmphp/Cache
|
||||
|
||||
Cache component for **Rmphp**
|
||||
|
||||
## Install
|
||||
|
||||
Stable version
|
||||
|
||||
```bash
|
||||
composer require rmphp/cache
|
||||
```
|
||||
```bash
|
||||
composer require rmphp/cache:"^1.0"
|
||||
```
|
||||
|
||||
|
||||
Dev version contains the latest changes
|
||||
|
||||
```bash
|
||||
composer require rmphp/cache:"1.x-dev"
|
||||
```
|
||||
19
composer.json
Normal file
19
composer.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "rmphp/cache",
|
||||
"license": "proprietary",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Yuri Zuev",
|
||||
"email": "y_zuev@mail.ru"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Rmphp\\Cache\\": "src/"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
111
src/Cache/Cache.php
Normal file
111
src/Cache/Cache.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
*/
|
||||
|
||||
|
||||
namespace Cache;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Cache implements CacheInterface {
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param int $defaultTime
|
||||
* @param int $subLength
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(
|
||||
private string $path,
|
||||
private readonly int $defaultTime = 120,
|
||||
private readonly int $subLength = 2
|
||||
){
|
||||
$this->path = rtrim($path, "/");
|
||||
if(!is_dir($this->path) && !mkdir($this->path, 0777, true)) throw new Exception("Error create path");
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function save(string $name, $value, int $expires = 0) : bool {
|
||||
if(empty($expires)) $expires = time() + $this->defaultTime;
|
||||
if(!$this->fileSave($name, $expires."::".$name."::".serialize($value))) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function getItem(string $key) {
|
||||
if($this->hasItem($key)){
|
||||
$data = explode("::", $this->fileRead($key), 3);
|
||||
return unserialize($data[2]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function hasItem(string $key) : bool {
|
||||
if (!empty($fileValue = $this->fileRead($key))){
|
||||
if(count($data = explode("::", $fileValue, 3)) != 3) return false;
|
||||
if(time() < (int)$data[0]) return true;
|
||||
}
|
||||
$this->fileDelete($key);
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @inheritDoc */
|
||||
public function get(string $name, callable $function, int $expires = 0) : mixed {
|
||||
if($this->hasItem($name)){
|
||||
$data = explode("::", $this->fileRead($name), 3);
|
||||
return unserialize($data[2]);
|
||||
}
|
||||
$value = $function();
|
||||
if(empty($expires)) $expires = time() + $this->defaultTime;
|
||||
$this->fileSave($name, $expires."::".$name."::".serialize($value));
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return object
|
||||
*/
|
||||
private function getPath(string $name) : object {
|
||||
$hexName = md5($name);
|
||||
$subPath = substr($hexName, 0, $this->subLength);
|
||||
return (object)[
|
||||
"path" => $this->path."/".$subPath,
|
||||
"file" => $this->path."/".$subPath."/".$hexName,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $data
|
||||
* @return bool
|
||||
*/
|
||||
private function fileSave(string $name, string $data) : bool {
|
||||
$pathObject = $this->getPath($name);
|
||||
if(!is_dir($pathObject->path)) mkdir($pathObject->path, 0777, true);
|
||||
if(!file_put_contents($pathObject->file, $data)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
private function fileRead(string $name) : string {
|
||||
$pathObject = $this->getPath($name);
|
||||
if (file_exists($pathObject->file) && $fileValue = file_get_contents($pathObject->file)) return $fileValue;
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function fileDelete($key) : bool {
|
||||
$pathObject = $this->getPath($key);
|
||||
if(file_exists($pathObject->file)) unlink($pathObject->file);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
40
src/Cache/CacheInterface.php
Normal file
40
src/Cache/CacheInterface.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Zuev Yuri
|
||||
*/
|
||||
|
||||
|
||||
namespace Cache;
|
||||
|
||||
interface CacheInterface {
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $value
|
||||
* @param int $expires
|
||||
* @return bool
|
||||
*/
|
||||
public function save(string $name, $value, int $expires = 0) : bool;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getItem(string $key);
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function hasItem(string $key) : bool;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param callable $function
|
||||
* @param int $expires
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name, callable $function, int $expires = 0): mixed;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user