Skip to content

Commit 81fcff2

Browse files
committed
Use Static
1 parent ae011c9 commit 81fcff2

File tree

9 files changed

+86
-88
lines changed

9 files changed

+86
-88
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codemommy/cachephp",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "CodeMommy CachePHP is a cache helper for web development.",
55
"keywords": [
66
"CodeMommy",

source/Cache.php

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
*/
1616
class Cache
1717
{
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;
18+
const TIMEOUT_ONE_SECOND = 1; // 1
19+
const TIMEOUT_ONE_MINUTE = 60; // 1 * 60
20+
const TIMEOUT_ONE_HOUR = 3600; // 1 * 60 * 60
21+
const TIMEOUT_ONE_DAY = 86400; // 1 * 60 * 60 * 24
22+
const TIMEOUT_ONE_MONTH = 2592000; // 1 * 60 * 60 * 24 * 30
23+
const TIMEOUT_ONE_QUARTER = 7776000; // 1 * 60 * 60 * 24 * 90
24+
const TIMEOUT_ONE_YEAR = 31536000; // 1 * 60 * 60 * 24 * 365
25+
const TIMEOUT_ONE_CENTURY = 3153600000; // 1 * 60 * 60 * 24 * 365 * 100
26+
const TIMEOUT_ONE_LIFE = 3153600000; // 1 * 60 * 60 * 24 * 365 * 100
2727

2828
const SERVER_LOCALHOST = 'localhost';
2929

@@ -35,23 +35,9 @@ class Cache
3535
const PORT_REDIS = 6379;
3636
const PORT_MEMCACHED = 11211;
3737

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-
}
38+
private static $config = null;
39+
private static $driver = null;
40+
private static $prefix = null;
5541

5642
/**
5743
* Get Key
@@ -60,9 +46,9 @@ public function __construct($config = null)
6046
*
6147
* @return string
6248
*/
63-
private function getKey($key)
49+
private static function getKey($key)
6450
{
65-
return $this->prefix . $key;
51+
return self::$prefix . $key;
6652
}
6753

6854
/**
@@ -72,9 +58,9 @@ private function getKey($key)
7258
*
7359
* @return bool
7460
*/
75-
private function isDriver($driver)
61+
private static function isDriver($driver)
7662
{
77-
if ($this->config['driver'] == $driver) {
63+
if (self::$config['driver'] == $driver) {
7864
return true;
7965
}
8066
return false;
@@ -83,30 +69,42 @@ private function isDriver($driver)
8369
/**
8470
* Start Driver
8571
*/
86-
private function startDriver()
72+
private static function startDriver()
8773
{
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']);
74+
if (self::$driver == null) {
75+
if (self::isDriver(self::DRIVER_REDIS)) {
76+
self::$driver = new Redis();
77+
self::$driver->connect(self::$config['server'], self::$config['port']);
78+
if (isset(self::$config['password'])) {
79+
self::$driver->auth(self::$config['password']);
9480
}
95-
if (isset($this->config['database'])) {
96-
$this->driver->select($this->config['database']);
81+
if (isset(self::$config['database'])) {
82+
self::$driver->select(self::$config['database']);
9783
}
9884
}
9985
}
10086
}
10187

88+
/**
89+
* Set Config
90+
*
91+
* @param null $config
92+
*/
93+
public static function setConfig($config = null)
94+
{
95+
self::$config = is_array($config) ? $config : array();
96+
self::$prefix = isset(self::$config['prefix']) ? strval(self::$config['prefix']) : '';
97+
self::$driver = null;
98+
}
99+
102100
/**
103101
* Close Driver
104102
* @return null
105103
*/
106-
public function closeDriver()
104+
public static function closeDriver()
107105
{
108-
if ($this->isDriver(self::DRIVER_REDIS)) {
109-
return $this->driver->close();
106+
if (self::isDriver(self::DRIVER_REDIS)) {
107+
return self::$driver->close();
110108
}
111109
return false;
112110
}
@@ -115,10 +113,10 @@ public function closeDriver()
115113
* Get Driver
116114
* @return null
117115
*/
118-
public function getDriver()
116+
public static function getDriver()
119117
{
120-
$this->startDriver();
121-
return $this->driver;
118+
self::startDriver();
119+
return self::$driver;
122120
}
123121

124122
/**
@@ -130,15 +128,15 @@ public function getDriver()
130128
*
131129
* @return mixed
132130
*/
133-
public function getData($key, $timeout, $function)
131+
public static function getData($key, $timeout, $function)
134132
{
135-
$key = $this->getKey($key);
136-
if ($this->isExist($key)) {
137-
return unserialize($this->readValue($key));
133+
$key = self::getKey($key);
134+
if (self::isExist($key)) {
135+
return unserialize(self::readValue($key));
138136
}
139137
$value = $function();
140138
$timeout = intval($timeout);
141-
$this->writeValue($key, serialize($value), $timeout);
139+
self::writeValue($key, serialize($value), $timeout);
142140
return $value;
143141
}
144142

@@ -149,12 +147,12 @@ public function getData($key, $timeout, $function)
149147
*
150148
* @return bool
151149
*/
152-
public function isExist($key)
150+
public static function isExist($key)
153151
{
154-
$key = $this->getKey($key);
155-
$this->startDriver();
156-
if ($this->isDriver(self::DRIVER_REDIS)) {
157-
return $this->driver->exists($key);
152+
$key = self::getKey($key);
153+
self::startDriver();
154+
if (self::isDriver(self::DRIVER_REDIS)) {
155+
return self::$driver->exists($key);
158156
}
159157
return false;
160158
}
@@ -166,12 +164,12 @@ public function isExist($key)
166164
*
167165
* @return bool
168166
*/
169-
public function delete($key)
167+
public static function delete($key)
170168
{
171-
$key = $this->getKey($key);
172-
$this->startDriver();
173-
if ($this->isDriver(self::DRIVER_REDIS)) {
174-
$result = $this->driver->delete($key);
169+
$key = self::getKey($key);
170+
self::startDriver();
171+
if (self::isDriver(self::DRIVER_REDIS)) {
172+
$result = self::$driver->delete($key);
175173
if ($result > 0) {
176174
return true;
177175
}
@@ -189,13 +187,13 @@ public function delete($key)
189187
*
190188
* @return bool
191189
*/
192-
public function writeValue($key, $value, $timeout = 0)
190+
public static function writeValue($key, $value, $timeout = 0)
193191
{
194-
$key = $this->getKey($key);
195-
$this->startDriver();
192+
$key = self::getKey($key);
193+
self::startDriver();
196194
$timeout = intval($timeout);
197-
if ($this->isDriver(self::DRIVER_REDIS)) {
198-
$this->driver->set($key, $value, $timeout);
195+
if (self::isDriver(self::DRIVER_REDIS)) {
196+
self::$driver->set($key, $value, $timeout);
199197
return true;
200198
}
201199
return false;
@@ -209,15 +207,15 @@ public function writeValue($key, $value, $timeout = 0)
209207
*
210208
* @return null
211209
*/
212-
public function readValue($key, $default = null)
210+
public static function readValue($key, $default = null)
213211
{
214-
$this->startDriver();
215-
if ($this->isDriver(self::DRIVER_REDIS)) {
216-
if (!$this->isExist($key)) {
212+
self::startDriver();
213+
if (self::isDriver(self::DRIVER_REDIS)) {
214+
if (!self::isExist($key)) {
217215
return $default;
218216
}
219-
$key = $this->getKey($key);
220-
return $this->driver->get($key);
217+
$key = self::getKey($key);
218+
return self::$driver->get($key);
221219
}
222220
return $default;
223221
}

test/closeDriver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
use CodeMommy\CachePHP\Cache;
1010

1111
$config = require_once(__DIR__ . '/config.php');
12-
$cache = new Cache($config);
13-
$cache->isExist('key');
14-
$cache->closeDriver();
12+
Cache::setConfig($config);
13+
Cache::delete('key');
14+
Cache::closeDriver();

test/delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
use CodeMommy\CachePHP\Cache;
1010

1111
$config = require_once(__DIR__ . '/config.php');
12-
$cache = new Cache($config);
13-
echo $cache->delete('key');
12+
Cache::setConfig($config);
13+
echo Cache::delete('key');

test/getData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use CodeMommy\CachePHP\Cache;
1010

1111
$config = require_once(__DIR__ . '/config.php');
12-
$cache = new Cache($config);
13-
$result = $cache->getData('key', $cache::TIMEOUT_ONE_MINUTE, function () {
12+
Cache::setConfig($config);
13+
$result = Cache::getData('key', Cache::TIMEOUT_ONE_MINUTE, function () {
1414
var_dump('No Cache');
1515
return 'OK';
1616
});

test/getDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
use CodeMommy\CachePHP\Cache;
1010

1111
$config = require_once(__DIR__ . '/config.php');
12-
$cache = new Cache($config);
13-
$driver = $cache->getDriver();
12+
Cache::setConfig($config);
13+
$driver = Cache::getDriver();
1414
echo $driver->get($config['prefix'] . 'key');

test/isExist.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
use CodeMommy\CachePHP\Cache;
1010

1111
$config = require_once(__DIR__ . '/config.php');
12-
$cache = new Cache($config);
13-
var_dump($cache->isExist('key'));
14-
var_dump($cache->isExist('hello'));
12+
Cache::setConfig($config);
13+
var_dump(Cache::isExist('key'));
14+
var_dump(Cache::isExist('hello'));

test/readValue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
use CodeMommy\CachePHP\Cache;
1010

1111
$config = require_once(__DIR__ . '/config.php');
12-
$cache = new Cache($config);
13-
echo $cache->readValue('key', 'default');
12+
Cache::setConfig($config);
13+
echo Cache::readValue('key', 'default');

test/writeValue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
use CodeMommy\CachePHP\Cache;
1010

1111
$config = require_once(__DIR__ . '/config.php');
12-
$cache = new Cache($config);
13-
$cache->writeValue('key', 'value', Cache::TIMEOUT_ONE_MINUTE);
12+
Cache::setConfig($config);
13+
Cache::writeValue('key', 'value', Cache::TIMEOUT_ONE_MINUTE);

0 commit comments

Comments
 (0)