Skip to content

Commit c919f5f

Browse files
committed
0.0.1
1 parent 0abd73c commit c919f5f

12 files changed

Lines changed: 404 additions & 0 deletions

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#IDE
2+
/.idea/
3+
4+
#Composer
5+
/vendor/
6+
/composer.lock

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# CodeMommy CachePHP
2+
3+
[![License](https://poser.pugx.org/CodeMommy/CachePHP/license?format=flat-square)](LICENSE)
4+
[![Download](https://poser.pugx.org/CodeMommy/CachePHP/downloads?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP)
5+
[![Stable](https://poser.pugx.org/CodeMommy/CachePHP/version?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP)
6+
[![Unstable](https://poser.pugx.org/CodeMommy/CachePHP/v/unstable?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP)
7+
[![composer.lock Available](https://poser.pugx.org/CodeMommy/CachePHP/composerlock?format=flat-square)](https://packagist.org/packages/CodeMommy/CachePHP)
8+
9+
10+
> CodeMommy CachePHP is a cache helper for web development.
11+
12+
It helps you to operate cookie easily.
13+
14+
Visit [CodeMommy Website](http://www.codemommy.com) or [Packagist](https://packagist.org/packages/CodeMommy/CachePHP) to get more information.
15+
16+
## Authors
17+
18+
| Name | Identity | Social |
19+
| :--- | :------- | :----- |
20+
| Candison November | Creator | [Website](http://www.kandisheng.com) - [GitHub](https://github.com/KanDisheng) |
21+
22+
## More
23+
24+
- [Feedback](https://github.com/CodeMommy/CachePHP/issues)
25+
- [About CodeMommy](https://github.com/CodeMommy/CodeMommy)

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "codemommy/cachephp",
3+
"version": "0.0.1",
4+
"description": "CodeMommy CachePHP is a cache helper for web development.",
5+
"keywords": [
6+
"CodeMommy",
7+
"CachePHP",
8+
"Cache",
9+
"PHP"
10+
],
11+
"license": "Apache 2.0",
12+
"homepage": "http://www.codemommy.com",
13+
"support": {
14+
"issues": "https://github.com/CodeMommy/CachePHP/issues",
15+
"source": "https://github.com/CodeMommy/CachePHP"
16+
},
17+
"authors": [
18+
{
19+
"name": "Candison November",
20+
"email": "kandisheng@163.com",
21+
"homepage": "http://www.kandisheng.com"
22+
}
23+
],
24+
"autoload": {
25+
"psr-4": {
26+
"CodeMommy\\CachePHP\\": "source/"
27+
}
28+
},
29+
"require": {
30+
"php": ">=5.3.0"
31+
}
32+
}

source/Cache.php

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<?php
2+
3+
/**
4+
* CodeMommy CachePHP
5+
* @author Candison November <www.kandisheng.com>
6+
*/
7+
8+
namespace CodeMommy\CachePHP;
9+
10+
use Redis;
11+
12+
/**
13+
* Class Cache
14+
* @package CodeMommy\CachePHP
15+
*/
16+
class Cache
17+
{
18+
const TIMEOUT_ONE_SECOND = 1;
19+
const TIMEOUT_ONE_MINUTE = 1 * 60;
20+
const TIMEOUT_ONE_HOUR = 1 * 60 * 60;
21+
const TIMEOUT_ONE_DAY = 1 * 60 * 60 * 24;
22+
const TIMEOUT_ONE_MONTH = 1 * 60 * 60 * 24 * 30;
23+
const TIMEOUT_ONE_QUARTER = 1 * 60 * 60 * 24 * 90;
24+
const TIMEOUT_ONE_YEAR = 1 * 60 * 60 * 24 * 365;
25+
const TIMEOUT_ONE_CENTURY = 1 * 60 * 60 * 24 * 365 * 100;
26+
const TIMEOUT_ONE_LIFE = 1 * 60 * 60 * 24 * 365 * 100;
27+
28+
const SERVER_LOCALHOST = 'localhost';
29+
30+
const DRIVER_REDIS = 'Redis';
31+
const DRIVER_MEMCACHED = 'Memcached';
32+
const DRIVER_APC = 'APC';
33+
const DRIVER_XCACHE = 'XCache';
34+
35+
const PORT_REDIS = 6379;
36+
const PORT_MEMCACHED = 11211;
37+
38+
private $config = null;
39+
private $driver = null;
40+
private $prefix = null;
41+
42+
/**
43+
* Cache constructor.
44+
*
45+
* @param null $config
46+
*/
47+
public function __construct($config = null)
48+
{
49+
$this->config = array();
50+
if (is_array($config)) {
51+
$this->config = $config;
52+
}
53+
$this->prefix = isset($this->config['prefix']) ? strval($this->config['prefix']) : '';
54+
}
55+
56+
/**
57+
* Get Key
58+
*
59+
* @param $key
60+
*
61+
* @return string
62+
*/
63+
private function getKey($key)
64+
{
65+
return $this->prefix . $key;
66+
}
67+
68+
/**
69+
* Is Driver
70+
*
71+
* @param $driver
72+
*
73+
* @return bool
74+
*/
75+
private function isDriver($driver)
76+
{
77+
if ($this->config['driver'] == $driver) {
78+
return true;
79+
}
80+
return false;
81+
}
82+
83+
/**
84+
* Start Driver
85+
*/
86+
private function startDriver()
87+
{
88+
if ($this->driver == null) {
89+
if ($this->isDriver(self::DRIVER_REDIS)) {
90+
$this->driver = new Redis();
91+
$this->driver->connect($this->config['server'], $this->config['port']);
92+
if (isset($this->config['password'])) {
93+
$this->driver->auth($this->config['password']);
94+
}
95+
if (isset($this->config['database'])) {
96+
$this->driver->select($this->config['database']);
97+
}
98+
}
99+
}
100+
}
101+
102+
/**
103+
* Close Driver
104+
* @return null
105+
*/
106+
public function closeDriver()
107+
{
108+
if ($this->isDriver(self::DRIVER_REDIS)) {
109+
return $this->driver->close();
110+
}
111+
return false;
112+
}
113+
114+
/**
115+
* Get Driver
116+
* @return null
117+
*/
118+
public function getDriver()
119+
{
120+
$this->startDriver();
121+
return $this->driver;
122+
}
123+
124+
/**
125+
* Get Data
126+
*
127+
* @param $key
128+
* @param $timeout
129+
* @param $function
130+
*
131+
* @return mixed
132+
*/
133+
public function getData($key, $timeout, $function)
134+
{
135+
$key = $this->getKey($key);
136+
if ($this->isExist($key)) {
137+
return unserialize($this->readValue($key));
138+
}
139+
$value = $function();
140+
$timeout = intval($timeout);
141+
$this->writeValue($key, serialize($value), $timeout);
142+
return $value;
143+
}
144+
145+
/**
146+
* Is Exist
147+
*
148+
* @param $key
149+
*
150+
* @return bool
151+
*/
152+
public function isExist($key)
153+
{
154+
$key = $this->getKey($key);
155+
$this->startDriver();
156+
if ($this->isDriver(self::DRIVER_REDIS)) {
157+
return $this->driver->exists($key);
158+
}
159+
return false;
160+
}
161+
162+
/**
163+
* Delete
164+
*
165+
* @param $key
166+
*
167+
* @return bool
168+
*/
169+
public function delete($key)
170+
{
171+
$key = $this->getKey($key);
172+
$this->startDriver();
173+
if ($this->isDriver(self::DRIVER_REDIS)) {
174+
$result = $this->driver->delete($key);
175+
if ($result > 0) {
176+
return true;
177+
}
178+
return false;
179+
}
180+
return false;
181+
}
182+
183+
/**
184+
* Write Value
185+
*
186+
* @param $key
187+
* @param $value
188+
* @param int $timeout
189+
*
190+
* @return bool
191+
*/
192+
public function writeValue($key, $value, $timeout = 0)
193+
{
194+
$key = $this->getKey($key);
195+
$this->startDriver();
196+
$timeout = intval($timeout);
197+
if ($this->isDriver(self::DRIVER_REDIS)) {
198+
$this->driver->set($key, $value, $timeout);
199+
return true;
200+
}
201+
return false;
202+
}
203+
204+
/**
205+
* Read Value
206+
*
207+
* @param $key
208+
* @param null $default
209+
*
210+
* @return null
211+
*/
212+
public function readValue($key, $default = null)
213+
{
214+
$this->startDriver();
215+
if ($this->isDriver(self::DRIVER_REDIS)) {
216+
if (!$this->isExist($key)) {
217+
return $default;
218+
}
219+
$key = $this->getKey($key);
220+
return $this->driver->get($key);
221+
}
222+
return $default;
223+
}
224+
}

test/closeDriver.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
require_once(__DIR__ . '/../source/Cache.php');
8+
9+
use CodeMommy\CachePHP\Cache;
10+
11+
$config = require_once(__DIR__ . '/config.php');
12+
$cache = new Cache($config);
13+
$cache->isExist('key');
14+
$cache->closeDriver();

test/config.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
require_once(__DIR__ . '/../source/Cache.php');
8+
9+
use CodeMommy\CachePHP\Cache;
10+
11+
// Redis
12+
return array(
13+
'driver' => Cache::DRIVER_REDIS,
14+
'server' => Cache::SERVER_LOCALHOST,
15+
'port' => Cache::PORT_REDIS,
16+
'password' => '',
17+
'database' => 0,
18+
'prefix' => 'CachePHP'
19+
);

test/delete.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
require_once(__DIR__ . '/../source/Cache.php');
8+
9+
use CodeMommy\CachePHP\Cache;
10+
11+
$config = require_once(__DIR__ . '/config.php');
12+
$cache = new Cache($config);
13+
echo $cache->delete('key');

test/getData.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
require_once(__DIR__ . '/../source/Cache.php');
8+
9+
use CodeMommy\CachePHP\Cache;
10+
11+
$config = require_once(__DIR__ . '/config.php');
12+
$cache = new Cache($config);
13+
$result = $cache->getData('key', $cache::TIMEOUT_ONE_MINUTE, function () {
14+
var_dump('No Cache');
15+
return 'OK';
16+
});
17+
echo $result;

test/getDriver.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
require_once(__DIR__ . '/../source/Cache.php');
8+
9+
use CodeMommy\CachePHP\Cache;
10+
11+
$config = require_once(__DIR__ . '/config.php');
12+
$cache = new Cache($config);
13+
$driver = $cache->getDriver();
14+
echo $driver->get($config['prefix'] . 'key');

test/isExist.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
require_once(__DIR__ . '/../source/Cache.php');
8+
9+
use CodeMommy\CachePHP\Cache;
10+
11+
$config = require_once(__DIR__ . '/config.php');
12+
$cache = new Cache($config);
13+
var_dump($cache->isExist('key'));
14+
var_dump($cache->isExist('hello'));

0 commit comments

Comments
 (0)