Skip to content

Commit 908b744

Browse files
committed
Initial release
1 parent 68a31da commit 908b744

File tree

7 files changed

+2042
-2
lines changed

7 files changed

+2042
-2
lines changed

.gitignore

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

README.md

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,159 @@
1-
# caching-performance-optimization
2-
caching-performance-optimization
1+
# PerformanceCache Package
2+
3+
The **PerformanceCache** package provides a robust caching solution that adheres to the PSR-16 (Simple Cache) interface, allowing developers to efficiently manage caching operations in PHP applications.
4+
5+
## Installation
6+
7+
You can install the PerformanceCache package via Composer. Run the following command in your terminal:
8+
9+
```bash
10+
composer require codecorners/performance-cache
11+
```
12+
13+
## Usage
14+
15+
### Initializing the Cache
16+
17+
To start using the cache, initialize an instance of `Cache`. By default, it uses `FileCacheHandler` for file-based caching:
18+
19+
```php
20+
use CodeCorner\PerformanceCache\Cache;
21+
use CodeCorner\PerformanceCache\FileCacheHandler;
22+
23+
// Initialize cache with default handler (FileCacheHandler)
24+
$cache = new Cache();
25+
```
26+
27+
You can optionally pass a custom cache handler to the constructor:
28+
29+
```php
30+
use CodeCorner\PerformanceCache\Cache;
31+
use App\CustomCacheHandler; // Replace with your custom cache handler
32+
33+
// Initialize cache with custom handler
34+
$customHandler = new CustomCacheHandler();
35+
$cache = new Cache($customHandler);
36+
```
37+
38+
### Basic Cache Operations
39+
40+
#### Setting a Cache Value
41+
42+
```php
43+
$key = 'my_key';
44+
$value = 'my_value';
45+
$ttl = 3600; // Optional TTL (time-to-live) in seconds
46+
47+
if ($cache->set($key, $value, $ttl)) {
48+
echo "Value successfully cached!\n";
49+
} else {
50+
echo "Failed to cache the value.\n";
51+
}
52+
```
53+
54+
#### Getting a Cached Value
55+
56+
```php
57+
$key = 'my_key';
58+
$defaultValue = 'default_value'; // Optional default value if key not found
59+
60+
$cachedValue = $cache->get($key, $defaultValue);
61+
62+
echo "Cached Value: $cachedValue\n";
63+
```
64+
65+
#### Deleting a Cached Value
66+
67+
```php
68+
$key = 'my_key';
69+
70+
if ($cache->delete($key)) {
71+
echo "Cache entry successfully deleted!\n";
72+
} else {
73+
echo "Failed to delete the cache entry.\n";
74+
}
75+
```
76+
77+
#### Clearing All Cached Values
78+
79+
```php
80+
if ($cache->clear()) {
81+
echo "Cache cleared successfully!\n";
82+
} else {
83+
echo "Failed to clear the cache.\n";
84+
}
85+
```
86+
87+
#### Working with Multiple Cache Entries
88+
89+
##### Getting Multiple Cache Entries
90+
91+
```php
92+
$keys = ['key1', 'key2', 'key3'];
93+
$defaultValue = 'default_value'; // Optional default value if any key is not found
94+
95+
$cachedValues = $cache->getMultiple($keys, $defaultValue);
96+
97+
foreach ($cachedValues as $key => $value) {
98+
echo "Key: $key, Value: $value\n";
99+
}
100+
```
101+
102+
##### Setting Multiple Cache Entries
103+
104+
```php
105+
$values = [
106+
'key1' => 'value1',
107+
'key2' => 'value2',
108+
'key3' => 'value3',
109+
];
110+
$ttl = 3600; // Optional TTL for all entries
111+
112+
if ($cache->setMultiple($values, $ttl)) {
113+
echo "Multiple values successfully cached!\n";
114+
} else {
115+
echo "Failed to cache multiple values.\n";
116+
}
117+
```
118+
119+
##### Deleting Multiple Cache Entries
120+
121+
```php
122+
$keysToDelete = ['key1', 'key2', 'key3'];
123+
124+
if ($cache->deleteMultiple($keysToDelete)) {
125+
echo "Multiple cache entries deleted successfully!\n";
126+
} else {
127+
echo "Failed to delete multiple cache entries.\n";
128+
}
129+
```
130+
131+
#### Checking if a Key Exists in Cache
132+
133+
```php
134+
$key = 'my_key';
135+
136+
if ($cache->has($key)) {
137+
echo "Key '$key' exists in cache.\n";
138+
} else {
139+
echo "Key '$key' does not exist in cache.\n";
140+
}
141+
```
142+
143+
### Error Handling
144+
145+
The `Cache` class provides basic error handling for cache operations. If an operation fails (e.g., cache read, write, delete), it logs the error message using `error_log()`.
146+
147+
## License
148+
149+
This package is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
150+
151+
## Author
152+
153+
Written by Yash Gupta.
154+
155+
---
156+
157+
Replace placeholders such as `Yash Gupta` with your actual name or preferred pseudonym. Ensure the `LICENSE` file is present in your project directory and contains the appropriate license text for distribution.
158+
159+
This README file provides comprehensive guidance for developers looking to integrate the **PerformanceCache** package into their PHP projects, covering installation, basic usage examples, error handling considerations, and licensing information. Adjust the examples and instructions as per your specific implementation and documentation style.

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "code-corner/performance-cache",
3+
"description": "A caching library for PHP performance optimization supporting multiple caching strategies.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Yash Gupta",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.4.2",
14+
"psr/simple-cache": ">=2.0.0"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^8.0"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"CodeCorner\\PerformanceCache\\": "src/"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"CodeCorner\\PerformanceCache\\Tests\\": "tests/"
27+
}
28+
},
29+
"minimum-stability": "stable"
30+
}

0 commit comments

Comments
 (0)