Skip to content

Commit 2c07973

Browse files
First Commit
0 parents  commit 2c07973

File tree

6 files changed

+263
-0
lines changed

6 files changed

+263
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
cache/

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
![LightCache](./docs/header.png)
2+
3+
# LightCache
4+
5+
A Fast, Easy and FileSystem based Cache for PHP.
6+
## About Author
7+
[Nabeel Ali](https://iconiccodes.com)
8+
9+
Website: [https://iconiccodes.com](https://iconiccodes.com)
10+
11+
Email: [mail2nabeelali@gmail.com](mailto:mail2nabeelali@gmail.com)
12+
13+
## Features
14+
15+
* Fast
16+
* Easy
17+
* Lightweight
18+
* Supports Expiration
19+
20+
21+
## Installtion
22+
```
23+
composer require nabeelalihashmi/LightCache
24+
```
25+
26+
## Basic Usage
27+
```
28+
$cache = new IconicCodes\LightCache\FilesCache;
29+
$cache->cachedir = __DIR__ . "/cache";
30+
31+
$cache->get('key', function () {
32+
return ["value", time() + 10]; // cache for 10 seconds
33+
});
34+
35+
$cache->delete('key');
36+
37+
```
38+
39+
## Methods
40+
41+
* `get`
42+
* `getValue`
43+
* `has`
44+
* `delete`
45+
* `set`
46+
47+
### get($key, $callback)
48+
Returns the value of the key. If the key is not found, the callback will be called and the value will be cached. If the key is found, the callback will not be called. The callback shall return an array with the value and the expiration time.
49+
50+
```
51+
$cache-get('key', function() {
52+
return ["value", time() + 10]; // cache for 10 seconds
53+
});
54+
```
55+
56+
This is probably most used function.
57+
58+
### getValue($key)
59+
Return value if key exists or cache is not exired. If key does not exist, returns false. If cache is expired, returns false.
60+
61+
### has($key)
62+
Returns true if key exists. Doesn't check cache expiration.
63+
64+
### delete($key)
65+
Deletes key from cache. This is probably the second most used function.
66+
67+
## set($key, $value, $expiration)
68+
Sets the value of the key. The expiration time is optional. If not set, the value will be cached for ever.
69+
70+
-------------------------
71+
72+
## License
73+
74+
LightCache is released under permissive licese with following conditions:
75+
76+
* It cannot be used to create adult apps.
77+
* It cannot be used to gambling apps.
78+
* It cannot be used to create apps having hate speech.
79+
80+
### MIT License
81+
82+
Copyright 2022 Nabeel Ali | IconicCodes.com
83+
84+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
85+
86+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
87+
88+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89+

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "nabeelalihashmi/lightcache",
3+
"description": "Lightweight Files Based Caching",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"IconicCodes\\LightCache\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Nabeel Ali",
14+
"email": "mail2nabeelali@gmail.com"
15+
}
16+
],
17+
"minimum-stability": "stable",
18+
"require": {}
19+
}

docs/header.png

42.8 KB
Loading

src/FilesCache.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace IconicCodes\LightCache;
4+
5+
6+
class FilesCache {
7+
8+
public $cachedir = "";
9+
public $cache_version = "1.0.0";
10+
11+
public function getValue($key) {
12+
$filename = $this->getHashFilename($key);
13+
if (file_exists($filename)) {
14+
$data = unserialize(file_get_contents($filename));
15+
$ttl = $data['t'];
16+
if ($ttl === 0) {
17+
return $data['d'];
18+
}
19+
20+
if ($ttl !== 0 && time() >= $ttl) {
21+
return false;
22+
}
23+
return $data['d'];
24+
}
25+
return false;
26+
}
27+
28+
private function sha($key) {
29+
return sha1($key);
30+
}
31+
32+
private function getHashFilename($filename, $makeDirs = false) {
33+
if (!file_exists($this->cachedir)) {
34+
mkdir($this->cachedir, 0777, true);
35+
}
36+
if (strpos($filename, '.')) {
37+
$parts = explode('.', $filename);
38+
$list = array_map(function ($part) {
39+
return $this->sha($part);
40+
$part;
41+
}, $parts);
42+
$outputFilename = implode('/', $list) . '.cache';
43+
44+
if ($makeDirs) {
45+
$dir = $this->cachedir . '/' . $outputFilename;
46+
if (!file_exists($dir)) {
47+
$path = dirname($dir);
48+
mkdir($path, 0777, true);
49+
}
50+
}
51+
} else {
52+
$outputFilename = $this->sha($filename) . '.cache';
53+
}
54+
55+
return $this->cachedir . '/' . $outputFilename;
56+
}
57+
58+
public function has($key) {
59+
$filename = $this->getHashFilename($key);
60+
return file_exists($filename);
61+
}
62+
63+
public function delete($key) {
64+
$filename = $this->getHashFilename($key);
65+
if (file_exists($filename)) {
66+
unlink($filename);
67+
}
68+
}
69+
70+
public function set($key, $value, $ttl = 0) {
71+
$cacheData = serialize([
72+
'd' => $value,
73+
't' => $ttl
74+
]);
75+
file_put_contents($this->getHashFilename($key, true), $cacheData);
76+
}
77+
78+
public function get($key, $callback) {
79+
$data = $this->getValue($key);
80+
if ($data !== false) {
81+
return $data;
82+
}
83+
$data = $callback();
84+
$this->set($key, $data[0], $data[1]);
85+
return $data[0];
86+
}
87+
}

test.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use IconicCodes\LightCache\FilesCache;
4+
5+
require 'vendor/autoload.php';
6+
7+
$cache = new FilesCache;
8+
9+
$cache->cachedir = __DIR__ . "/cache";
10+
11+
$item = $cache->get('test_cache_1', function() {
12+
return [ uniqid(), time() + 5];
13+
});
14+
15+
$count = 0;
16+
$oldItem = $item;
17+
while ($count < 7) {
18+
$item = $cache->get('test_cache_1', function() {
19+
return [ uniqid(), time() + 5];
20+
});
21+
22+
if($count < 5) {
23+
$test = 'testEq';
24+
$exp = 'cached for';
25+
} else {
26+
$test = 'testNotEq';
27+
$exp = 'expired after';
28+
}
29+
$test_num = $count + 1;
30+
$test($item, $oldItem, "Test ${test_num} \nExpected item to be $exp 5 seconds");
31+
$count++;
32+
sleep(1);
33+
}
34+
35+
36+
37+
38+
function testNotEq($expect, $output, $message) {
39+
echo "\n$message \n";
40+
echo 'not expected value: ' . $expect . "\n";
41+
echo 'output value: ' . $output . "\n";
42+
43+
if ($expect !== $output) {
44+
echo "TEST OK \n";
45+
} else {
46+
echo "TEST FAIL \n";
47+
}
48+
49+
echo "\n-----------------------------------\n";
50+
51+
}
52+
53+
function testEq($expect, $output, $message) {
54+
echo "\n$message \n";
55+
echo 'expected value: ' . $expect . "\n";
56+
echo 'output value: ' . $output . "\n";
57+
58+
if ($expect === $output) {
59+
echo "TEST OK \n";
60+
} else {
61+
echo "TEST FAIL \n";
62+
}
63+
64+
echo "\n-----------------------------------\n";
65+
66+
}

0 commit comments

Comments
 (0)