Skip to content

Commit c965071

Browse files
committed
Update composer, refactor implementation
1 parent 15ae3fd commit c965071

File tree

8 files changed

+205
-151
lines changed

8 files changed

+205
-151
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
nbproject/private/
2+
composer.lock
3+
nbproject/
4+
vendor/

FileCache.php

Lines changed: 0 additions & 106 deletions
This file was deleted.

MemoryCache.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

composer.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
{
2-
"name": "vendor/simplePhpCache",
3-
"description": "This very small lib is for caching in php apps.",
2+
"name": "jtk/simplePhpCache",
3+
"description": "Very small and simple lib for caching",
4+
"keywords": ["cache", "filecache", "memorycache", "arraycache"],
5+
"homepage": "http://github.com/jtkDvlp/simplePhpCache",
6+
"type": "library",
7+
"license": "MIT",
48
"authors": [
59
{
6-
"name": "JT Knabenschuh",
10+
"name": "jtkDvlp",
711
"email": "[email protected]"
812
}
913
],
10-
"require": {}
14+
"require": {
15+
"php": ">=5.5.0"
16+
},
17+
"autoload": {
18+
"psr-4": {"jtk\\simplePhpCache\\": "src"}
19+
}
1120
}

example.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?php
1+
<?php
2+
use jtk\simplePhpCache\FileCache;
3+
use jtk\simplePhpCache\ArrayCache;
24

3-
require_once('./FileCache.php');
4-
require_once('./MemoryCache.php');
5+
require __DIR__ . '/vendor/autoload.php';
56

67
session_start();
78

@@ -40,7 +41,7 @@ function example($cache)
4041
mkdir('./cache/');
4142
}
4243

43-
$filecache = new FileCache('./cache/', 50);
44+
$filecache = new FileCache(__DIR__.'/cache/', 50);
4445
//$cache->clear();
4546
example($filecache);
4647

@@ -54,7 +55,7 @@ function example($cache)
5455
$_SESSION['cache'] = [];
5556
}
5657

57-
$sessioncache = new MemoryCache(
58+
$sessioncache = new ArrayCache(
5859
$_SESSION['cache']);
5960
//$cache->clear();
6061
example($sessioncache);

src/ArrayCache.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php namespace jtk\simplePhpCache;
2+
3+
class ArrayCache extends Cache
4+
{
5+
/** @var array */
6+
private $array;
7+
8+
9+
/**
10+
* @param array $array
11+
*/
12+
public function __construct(&$array)
13+
{
14+
$this->array = &$array;
15+
}
16+
17+
/**
18+
* @param string $identifier
19+
* @return mixed
20+
*/
21+
public function get($identifier)
22+
{
23+
return $this->array[$identifier];
24+
}
25+
26+
/**
27+
* @param string $identifier
28+
* @return boolean
29+
*/
30+
public function has($identifier)
31+
{
32+
return isset($this->array[$identifier]);
33+
}
34+
35+
/**
36+
* @param string $identifier
37+
* @param mixed $data
38+
* @return mixed
39+
*/
40+
public function set($identifier, $data)
41+
{
42+
return $this->array[$identifier] = $data;
43+
}
44+
45+
public function clear()
46+
{
47+
$this->array = [];
48+
}
49+
}

Cache.php renamed to src/Cache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php namespace jtk\simplePhpCache;
22

33
abstract class Cache
44
{
@@ -7,6 +7,10 @@ public abstract function has($identifier);
77
public abstract function set($identifier, $data);
88
public abstract function clear();
99

10+
/**
11+
* @param callable $func
12+
* @return callable
13+
*/
1014
public function wrap($func)
1115
{
1216
return function() use ($func)

0 commit comments

Comments
 (0)