Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature to autodelete expired items #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ For erasing cached data are these three methods available:
?>
```

- `autoEraseExpired(<$flag>)` Expired items will automatically erase
when calling `isCached()`, `retreive()`, and `retreiveAll()`

### Check cached data ###

`isCached($key)`
Expand Down Expand Up @@ -248,4 +251,4 @@ cache/7505d64a54e061b7acd54ccd58b49dc43500b635.cache
## Credits ##

Copyright (c) 2011-2013 - Programmed by Christian Metz / [@cosenary](http://twitter.com/cosenary)
Released under the [BSD License](http://www.opensource.org/licenses/bsd-license.php).
Released under the [BSD License](http://www.opensource.org/licenses/bsd-license.php).
34 changes: 34 additions & 0 deletions cache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class Cache {
*/
private $_extension = '.cache';

/**
* Determines if expired items are auto erased
*
* @var boolean
*/

private $_autoEraseExpired = FALSE;


/**
* Default constructor
*
Expand All @@ -59,6 +68,10 @@ public function __construct($config = null) {
* @return boolean
*/
public function isCached($key) {
if (TRUE === $this->autoEraseExpired){
$this->eraseExpired();
}

if (false != $this->_loadCache()) {
$cachedData = $this->_loadCache();
return isset($cachedData[$key]['data']);
Expand Down Expand Up @@ -98,6 +111,11 @@ public function store($key, $data, $expiration = 0) {
* @return string
*/
public function retrieve($key, $timestamp = false) {

if (TRUE === $this->autoEraseExpired){
$this->eraseExpired();
}

$cachedData = $this->_loadCache();
(false === $timestamp) ? $type = 'data' : $type = 'time';
if (!isset($cachedData[$key][$type])) return null;
Expand All @@ -111,6 +129,11 @@ public function retrieve($key, $timestamp = false) {
* @return array
*/
public function retrieveAll($meta = false) {

if (TRUE === $this->autoEraseExpired){
$this->eraseExpired();
}

if ($meta === false) {
$results = array();
$cachedData = $this->_loadCache();
Expand Down Expand Up @@ -209,6 +232,17 @@ public function getCacheDir() {
}
}


/**
* Set auto erase behavior
*
* @return boolean
*/

public function autoEraseExpired($flag){
$this->autoEraseExpired = $flag;
return $flag;
}
/**
* Get the filename hash
*
Expand Down