-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0abd73c
commit c919f5f
Showing
12 changed files
with
404 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#IDE | ||
/.idea/ | ||
|
||
#Composer | ||
/vendor/ | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# CodeMommy CachePHP | ||
|
||
[![License](https://poser.pugx.org/CodeMommy/CachePHP/license?format=flat-square)](LICENSE) | ||
[![Download](https://poser.pugx.org/CodeMommy/CachePHP/downloads?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP) | ||
[![Stable](https://poser.pugx.org/CodeMommy/CachePHP/version?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP) | ||
[![Unstable](https://poser.pugx.org/CodeMommy/CachePHP/v/unstable?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP) | ||
[![composer.lock Available](https://poser.pugx.org/CodeMommy/CachePHP/composerlock?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP) | ||
|
||
|
||
> CodeMommy CachePHP is a cache helper for web development. | ||
It helps you to operate cookie easily. | ||
|
||
Visit [CodeMommy Website](http://www.codemommy.com) or [Packagist](https://packagist.org/packages/CodeMommy/CachePHP) to get more information. | ||
|
||
## Authors | ||
|
||
| Name | Identity | Social | | ||
| :--- | :------- | :----- | | ||
| Candison November | Creator | [Website](http://www.kandisheng.com) - [GitHub](https://github.com/KanDisheng) | | ||
|
||
## More | ||
|
||
- [Feedback](https://github.com/CodeMommy/CachePHP/issues) | ||
- [About CodeMommy](https://github.com/CodeMommy/CodeMommy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "codemommy/cachephp", | ||
"version": "0.0.1", | ||
"description": "CodeMommy CachePHP is a cache helper for web development.", | ||
"keywords": [ | ||
"CodeMommy", | ||
"CachePHP", | ||
"Cache", | ||
"PHP" | ||
], | ||
"license": "Apache 2.0", | ||
"homepage": "http://www.codemommy.com", | ||
"support": { | ||
"issues": "https://github.com/CodeMommy/CachePHP/issues", | ||
"source": "https://github.com/CodeMommy/CachePHP" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Candison November", | ||
"email": "kandisheng@163.com", | ||
"homepage": "http://www.kandisheng.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"CodeMommy\\CachePHP\\": "source/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=5.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
<?php | ||
|
||
/** | ||
* CodeMommy CachePHP | ||
* @author Candison November <www.kandisheng.com> | ||
*/ | ||
|
||
namespace CodeMommy\CachePHP; | ||
|
||
use Redis; | ||
|
||
/** | ||
* Class Cache | ||
* @package CodeMommy\CachePHP | ||
*/ | ||
class Cache | ||
{ | ||
const TIMEOUT_ONE_SECOND = 1; | ||
const TIMEOUT_ONE_MINUTE = 1 * 60; | ||
const TIMEOUT_ONE_HOUR = 1 * 60 * 60; | ||
const TIMEOUT_ONE_DAY = 1 * 60 * 60 * 24; | ||
const TIMEOUT_ONE_MONTH = 1 * 60 * 60 * 24 * 30; | ||
const TIMEOUT_ONE_QUARTER = 1 * 60 * 60 * 24 * 90; | ||
const TIMEOUT_ONE_YEAR = 1 * 60 * 60 * 24 * 365; | ||
const TIMEOUT_ONE_CENTURY = 1 * 60 * 60 * 24 * 365 * 100; | ||
const TIMEOUT_ONE_LIFE = 1 * 60 * 60 * 24 * 365 * 100; | ||
|
||
const SERVER_LOCALHOST = 'localhost'; | ||
|
||
const DRIVER_REDIS = 'Redis'; | ||
const DRIVER_MEMCACHED = 'Memcached'; | ||
const DRIVER_APC = 'APC'; | ||
const DRIVER_XCACHE = 'XCache'; | ||
|
||
const PORT_REDIS = 6379; | ||
const PORT_MEMCACHED = 11211; | ||
|
||
private $config = null; | ||
private $driver = null; | ||
private $prefix = null; | ||
|
||
/** | ||
* Cache constructor. | ||
* | ||
* @param null $config | ||
*/ | ||
public function __construct($config = null) | ||
{ | ||
$this->config = array(); | ||
if (is_array($config)) { | ||
$this->config = $config; | ||
} | ||
$this->prefix = isset($this->config['prefix']) ? strval($this->config['prefix']) : ''; | ||
} | ||
|
||
/** | ||
* Get Key | ||
* | ||
* @param $key | ||
* | ||
* @return string | ||
*/ | ||
private function getKey($key) | ||
{ | ||
return $this->prefix . $key; | ||
} | ||
|
||
/** | ||
* Is Driver | ||
* | ||
* @param $driver | ||
* | ||
* @return bool | ||
*/ | ||
private function isDriver($driver) | ||
{ | ||
if ($this->config['driver'] == $driver) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Start Driver | ||
*/ | ||
private function startDriver() | ||
{ | ||
if ($this->driver == null) { | ||
if ($this->isDriver(self::DRIVER_REDIS)) { | ||
$this->driver = new Redis(); | ||
$this->driver->connect($this->config['server'], $this->config['port']); | ||
if (isset($this->config['password'])) { | ||
$this->driver->auth($this->config['password']); | ||
} | ||
if (isset($this->config['database'])) { | ||
$this->driver->select($this->config['database']); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Close Driver | ||
* @return null | ||
*/ | ||
public function closeDriver() | ||
{ | ||
if ($this->isDriver(self::DRIVER_REDIS)) { | ||
return $this->driver->close(); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Get Driver | ||
* @return null | ||
*/ | ||
public function getDriver() | ||
{ | ||
$this->startDriver(); | ||
return $this->driver; | ||
} | ||
|
||
/** | ||
* Get Data | ||
* | ||
* @param $key | ||
* @param $timeout | ||
* @param $function | ||
* | ||
* @return mixed | ||
*/ | ||
public function getData($key, $timeout, $function) | ||
{ | ||
$key = $this->getKey($key); | ||
if ($this->isExist($key)) { | ||
return unserialize($this->readValue($key)); | ||
} | ||
$value = $function(); | ||
$timeout = intval($timeout); | ||
$this->writeValue($key, serialize($value), $timeout); | ||
return $value; | ||
} | ||
|
||
/** | ||
* Is Exist | ||
* | ||
* @param $key | ||
* | ||
* @return bool | ||
*/ | ||
public function isExist($key) | ||
{ | ||
$key = $this->getKey($key); | ||
$this->startDriver(); | ||
if ($this->isDriver(self::DRIVER_REDIS)) { | ||
return $this->driver->exists($key); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Delete | ||
* | ||
* @param $key | ||
* | ||
* @return bool | ||
*/ | ||
public function delete($key) | ||
{ | ||
$key = $this->getKey($key); | ||
$this->startDriver(); | ||
if ($this->isDriver(self::DRIVER_REDIS)) { | ||
$result = $this->driver->delete($key); | ||
if ($result > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Write Value | ||
* | ||
* @param $key | ||
* @param $value | ||
* @param int $timeout | ||
* | ||
* @return bool | ||
*/ | ||
public function writeValue($key, $value, $timeout = 0) | ||
{ | ||
$key = $this->getKey($key); | ||
$this->startDriver(); | ||
$timeout = intval($timeout); | ||
if ($this->isDriver(self::DRIVER_REDIS)) { | ||
$this->driver->set($key, $value, $timeout); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Read Value | ||
* | ||
* @param $key | ||
* @param null $default | ||
* | ||
* @return null | ||
*/ | ||
public function readValue($key, $default = null) | ||
{ | ||
$this->startDriver(); | ||
if ($this->isDriver(self::DRIVER_REDIS)) { | ||
if (!$this->isExist($key)) { | ||
return $default; | ||
} | ||
$key = $this->getKey($key); | ||
return $this->driver->get($key); | ||
} | ||
return $default; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* @author Candison November <www.kandisheng.com> | ||
*/ | ||
|
||
require_once(__DIR__ . '/../source/Cache.php'); | ||
|
||
use CodeMommy\CachePHP\Cache; | ||
|
||
$config = require_once(__DIR__ . '/config.php'); | ||
$cache = new Cache($config); | ||
$cache->isExist('key'); | ||
$cache->closeDriver(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/** | ||
* @author Candison November <www.kandisheng.com> | ||
*/ | ||
|
||
require_once(__DIR__ . '/../source/Cache.php'); | ||
|
||
use CodeMommy\CachePHP\Cache; | ||
|
||
// Redis | ||
return array( | ||
'driver' => Cache::DRIVER_REDIS, | ||
'server' => Cache::SERVER_LOCALHOST, | ||
'port' => Cache::PORT_REDIS, | ||
'password' => '', | ||
'database' => 0, | ||
'prefix' => 'CachePHP' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
/** | ||
* @author Candison November <www.kandisheng.com> | ||
*/ | ||
|
||
require_once(__DIR__ . '/../source/Cache.php'); | ||
|
||
use CodeMommy\CachePHP\Cache; | ||
|
||
$config = require_once(__DIR__ . '/config.php'); | ||
$cache = new Cache($config); | ||
echo $cache->delete('key'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/** | ||
* @author Candison November <www.kandisheng.com> | ||
*/ | ||
|
||
require_once(__DIR__ . '/../source/Cache.php'); | ||
|
||
use CodeMommy\CachePHP\Cache; | ||
|
||
$config = require_once(__DIR__ . '/config.php'); | ||
$cache = new Cache($config); | ||
$result = $cache->getData('key', $cache::TIMEOUT_ONE_MINUTE, function () { | ||
var_dump('No Cache'); | ||
return 'OK'; | ||
}); | ||
echo $result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* @author Candison November <www.kandisheng.com> | ||
*/ | ||
|
||
require_once(__DIR__ . '/../source/Cache.php'); | ||
|
||
use CodeMommy\CachePHP\Cache; | ||
|
||
$config = require_once(__DIR__ . '/config.php'); | ||
$cache = new Cache($config); | ||
$driver = $cache->getDriver(); | ||
echo $driver->get($config['prefix'] . 'key'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* @author Candison November <www.kandisheng.com> | ||
*/ | ||
|
||
require_once(__DIR__ . '/../source/Cache.php'); | ||
|
||
use CodeMommy\CachePHP\Cache; | ||
|
||
$config = require_once(__DIR__ . '/config.php'); | ||
$cache = new Cache($config); | ||
var_dump($cache->isExist('key')); | ||
var_dump($cache->isExist('hello')); |
Oops, something went wrong.