Skip to content

Commit

Permalink
trait State
Browse files Browse the repository at this point in the history
  • Loading branch information
shabeer-ali-m committed Jul 11, 2021
1 parent b552e5a commit 712a177
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"minimum-stability": "stable",
"version": "0.0.8",
"version": "0.0.9",
"require": {
"php": ">=5.3.0"
},
Expand Down
38 changes: 38 additions & 0 deletions src/SuperCache/State.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* State.php
*
* @copyright MIT
* @author Shabeer Ali M https://github.com/shabeer-ali-m
* @since 0.0.9
*
*/

namespace SuperCache;

trait State
{

/**
* setState
* @param [array] $data
*/
public function setState($data)
{
foreach($data as $k => $v) {
$this->{$k} = $v;
}
}

/**
* __set_state
* @param [array] $data
*/
public static function __set_state($data)
{
$self = new self();
$self->setState($data);
return $self;
}
}
2 changes: 1 addition & 1 deletion src/SuperCache/SuperCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @copyright MIT
* @author Shabeer Ali M https://github.com/shabeer-ali-m
* @since 0.0.1
* @since 0.0.9
*
*/

Expand Down

0 comments on commit 712a177

Please sign in to comment.