From 712a177c8db76c74e139481b9081f6b9e45d061c Mon Sep 17 00:00:00 2001 From: Shabeer Ali M Date: Sun, 11 Jul 2021 13:29:11 +0530 Subject: [PATCH] trait State --- README.md | 45 +++++++++++++++++++++++++++++++++++ composer.json | 2 +- src/SuperCache/State.php | 38 +++++++++++++++++++++++++++++ src/SuperCache/SuperCache.php | 2 +- 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/SuperCache/State.php diff --git a/README.md b/README.md index 6c63a31..47d0027 100644 --- a/README.md +++ b/README.md @@ -73,5 +73,50 @@ sCache::cache('myKey')->destroy(); sCache::cache('myKey')->clearAll(); ``` +### Cache Your Class + +You can cache any class to super cache. To bind data you need to use trait class `State` by adding `use State`, or you can do via your custom set_state. + +```php +use SuperCache\SuperCache as sCache; +use SuperCache\State as State; + +sCache::setPath('youCacheLocation'); + +Class MyClass +{ + /** + * This trait class will bind data from cache to your class + */ + use State; + + private $firstName; + + private $lastName; + + public function setName($firstName, $lastName) + { + $this->firstName = $firstName; + $this->lastName = $lastName; + } + + public function getName() + { + return $this->firstName . " ". $this->lastName; + } +} + +// Creating Object of your class +$myObject = new MyClass; +$myObject->setName("John", "Doe"); + +// Saving your object in SuperCache +sCache::cache('myObject')->set($myObject); + +// Retrieving your object from Cache +$cacheObject = sCache::cache('myObject')->get(); +echo $myObject->getName(); +``` + ## License The MIT License (MIT). Please see [License File](LICENSE) for more information. diff --git a/composer.json b/composer.json index ee8d9db..87ad5d4 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "minimum-stability": "stable", - "version": "0.0.8", + "version": "0.0.9", "require": { "php": ">=5.3.0" }, diff --git a/src/SuperCache/State.php b/src/SuperCache/State.php new file mode 100644 index 0000000..256ecd6 --- /dev/null +++ b/src/SuperCache/State.php @@ -0,0 +1,38 @@ + $v) { + $this->{$k} = $v; + } + } + + /** + * __set_state + * @param [array] $data + */ + public static function __set_state($data) + { + $self = new self(); + $self->setState($data); + return $self; + } +} \ No newline at end of file diff --git a/src/SuperCache/SuperCache.php b/src/SuperCache/SuperCache.php index 16a2e99..ccbbc5b 100644 --- a/src/SuperCache/SuperCache.php +++ b/src/SuperCache/SuperCache.php @@ -5,7 +5,7 @@ * * @copyright MIT * @author Shabeer Ali M https://github.com/shabeer-ali-m - * @since 0.0.1 + * @since 0.0.9 * */