Skip to content

Commit c205455

Browse files
committed
Initial commit
0 parents  commit c205455

13 files changed

+771
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

Guardfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
guard 'phpunit', :tests_path => 'tests', :cli => '--colors' do
2+
# Watch tests files
3+
watch(%r{^tests/.+Test\.php$})
4+
5+
# Watch library files and run their tests
6+
watch(%r{^src/Domino/CacheStore/(.+)\.php$}) { |m| "tests/Domino/CacheStore/Tests/#{m[1]}Test.php" }
7+
end

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Domino Data Cache
2+
=================
3+
4+
provides a generic way to cache any data.
5+
6+
- It provide common interface for cache library.
7+
- support cache library APC and memcached.
8+
- support namespace. (use namespace delete)
9+
10+
11+
Install
12+
-------
13+
14+
using Composer(recommended):
15+
16+
```javascript
17+
{
18+
"require": {
19+
"SNakano/DataCache": "dev-master"
20+
}
21+
}
22+
```
23+
or cloning this repository.
24+
25+
26+
Usage
27+
-----
28+
29+
```php
30+
// configure cache setting.
31+
Domino\Factory::setOptions(
32+
array(
33+
array(
34+
'storage' => 'apc'
35+
'default_ttl' => 360
36+
),
37+
array(
38+
'strage' => 'memcached',
39+
'prefix' => 'domino_test',
40+
'default_ttl' => 360,
41+
'servers' => array(
42+
array('server1', 11211, 20),
43+
array('server2', 11211, 80)
44+
)
45+
)
46+
)
47+
);
48+
49+
// factory cache storage
50+
$storage = Domino\Factory::factory('memcached');
51+
52+
// set data
53+
$storage->set('ns1', 'key1', 'value1');
54+
$storage->set('ns1', 'key2', 'value2');
55+
$storage->set('ns2', 'key1', 'value1');
56+
$storage->set('ns2', 'key2', 'value2', 10); // specify ttl
57+
58+
// get data
59+
$storage->get('ns1', 'key1');
60+
# => 'value1'
61+
62+
// delete by namespace and key
63+
$storage->clear("ns1", 'key1');
64+
$storage->get('ns1', 'key1');
65+
# => null
66+
67+
// delete by namespace
68+
$storage->clearByNamespace('ns1');
69+
$storage->get('ns1', 'key2');
70+
# => null
71+
$storage->get('ns2', 'key1');
72+
# => 'value1'
73+
74+
// delete all
75+
$storage->clearAll();
76+
$storage->get('ns2', 'key1');
77+
# => null
78+
```
79+
80+
License
81+
-------
82+
83+
MIT license.

composer.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "SNakano/CacheStore",
3+
"description": "provides a generic way to cache any data",
4+
"license": "MIT",
5+
"keywords": ["cache", "memcached", "apc"],
6+
"authors": [
7+
{
8+
"name": "Shinya Nakano",
9+
"email": "s.nakano@do-mino.jp"
10+
}
11+
],
12+
"autoload": {
13+
"psr-0": {
14+
"Domino\\CacheStore\\" : "src/"
15+
}
16+
},
17+
"require": {
18+
"php": ">=5.3.3"
19+
},
20+
"suggest": {
21+
"ext-apc": "APC >= 3.1.6 to use the APC storage adapter",
22+
"ext-memcached": "Memcached >= 1.0.0 to use the Memcached storage adapter"
23+
}
24+
}

phpunit.xml.dist

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false"
11+
bootstrap="tests/bootstrap.php"
12+
>
13+
<testsuites>
14+
<testsuite name="CacheStore Test Suite">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
<filter>
19+
<whitelist>
20+
<directory>./src/</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

src/Domino/CacheStore/Factory.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Domino Cache Store
4+
*
5+
* @copyright Copyright (c) 2013 Domino Co. Ltd.
6+
* @license MIT
7+
* @package Domino_CacheStore
8+
*/
9+
10+
namespace Domino\CacheStore;
11+
12+
/**
13+
* Domino Cache Store Factory
14+
*
15+
* @package Domino_CacheStore
16+
* @subpackage Storage
17+
*/
18+
class Factory
19+
{
20+
/**
21+
* Cache store storage options
22+
* @var array
23+
*/
24+
private static $options = array();
25+
26+
/**
27+
* Set cache store storage options
28+
* @param array $options cache store storage options
29+
*/
30+
public static function setOptions($options = array())
31+
{
32+
self::$options = $options;
33+
}
34+
35+
/**
36+
* Get cache store storage options
37+
* @return array cache store storage options
38+
*/
39+
public static function getOptions()
40+
{
41+
return self::$options;
42+
}
43+
44+
/**
45+
* Set cache store storage option
46+
* @param array $option cache store storage option
47+
*/
48+
public static function setOption($option = array())
49+
{
50+
self::$options[] = $option;
51+
}
52+
53+
/**
54+
* Get cache store storage option
55+
* @param string $storage_type cache store storage type (eg. 'apc', 'memcached')
56+
* @return array cache store storage option
57+
*/
58+
public static function getOption($storage_type)
59+
{
60+
foreach (self::$options as $option) {
61+
if ($option['storage'] == $storage_type) {
62+
return $option;
63+
}
64+
}
65+
66+
return false;
67+
}
68+
69+
/**
70+
* Clear cache store storage options
71+
*/
72+
public static function clearOptions()
73+
{
74+
self::$options = array();
75+
}
76+
77+
/**
78+
* Instantiate a cache storage
79+
* @param string $storage_type cache store storage type (eg. 'apc', 'memcached')
80+
* @return Storage cache store storage instance
81+
*/
82+
public static function factory($storage_type)
83+
{
84+
$storage_class = 'Domino\CacheStore\Storage\\' . ucfirst($storage_type);
85+
86+
return new $storage_class(self::getOption($storage_type));
87+
}
88+
}

src/Domino/CacheStore/Storage/Apc.php

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Domino Cache Store
4+
*
5+
* @copyright Copyright (c) 2013 Domino Co. Ltd.
6+
* @license MIT
7+
* @package Domino_CacheStore
8+
*/
9+
10+
namespace Domino\CacheStore\Storage;
11+
12+
/**
13+
* Domino Cache Store Storage for APC
14+
*
15+
* @package Domino_CacheStore
16+
* @subpackage Storage
17+
*/
18+
class Apc implements StorageInterface
19+
{
20+
/**
21+
* Namespace separator
22+
*/
23+
const NAMESPACE_SEPARATOR = '\\';
24+
25+
/**
26+
* Default expiration time (sec)
27+
* @var integer
28+
*/
29+
private $default_ttl = 360;
30+
31+
/**
32+
* Constructor
33+
* @param array $option cache store storage option
34+
*/
35+
public function __construct($option = array())
36+
{
37+
$this->default_ttl = $option['default_ttl'];
38+
}
39+
40+
/**
41+
* Set item
42+
* @param string $namespace
43+
* @param string $key
44+
* @param mixed $value
45+
* @param integer $ttl expiration time (sec)
46+
*/
47+
public function set($namespace, $key, $value, $ttl = null)
48+
{
49+
$store_key = $this->generateStoreKey($namespace, $key);
50+
$expire = is_null($ttl) ? $this->default_ttl : $ttl;
51+
52+
return apc_store($store_key, $value, $expire);
53+
}
54+
55+
/**
56+
* Get item
57+
* @param string $namespace
58+
* @param string $key
59+
* @return mixed stored item
60+
*/
61+
public function get($namespace, $key)
62+
{
63+
$store_key = $this->generateStoreKey($namespace, $key);
64+
65+
return apc_exists($store_key) ? apc_fetch($store_key) : null;
66+
}
67+
68+
/**
69+
* Clear item
70+
* @param string $namespace
71+
* @param string $key
72+
* @return boolean success or failure
73+
*/
74+
public function clear($namespace, $key)
75+
{
76+
$store_key = $this->generateStoreKey($namespace, $key);
77+
78+
return apc_delete($store_key);
79+
}
80+
81+
/**
82+
* Clear by namespace
83+
* @param string $namespace
84+
* @return boolean success or failure
85+
*/
86+
public function clearByNamespace($namespace)
87+
{
88+
$pattern = '/^' . preg_quote($namespace . self::NAMESPACE_SEPARATOR) . '/';
89+
90+
return apc_delete(new \APCIterator('user', $pattern, APC_ITER_VALUE));
91+
}
92+
93+
/**
94+
* Clear all
95+
* @return boolean success or failure
96+
*/
97+
public function clearAll()
98+
{
99+
return apc_clear_cache("user");
100+
}
101+
102+
/**
103+
* Generate internal store key
104+
* @param string $namespace
105+
* @param string $key
106+
* @return string internal store key
107+
*/
108+
private function generateStoreKey($namespace, $key)
109+
{
110+
return $namespace . self::NAMESPACE_SEPARATOR . $key;
111+
}
112+
}

0 commit comments

Comments
 (0)