You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+261-30
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,80 @@
1
1
# Strongly typed cache objects
2
2
3
-
[](https://packagist.org/packages/19932449-eribloo/laravel-cache-objects)
[](https://packagist.org/packages/eribloo/laravel-cache-objects)
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
8
+
Introducing Laravel package that simplifies cache management by allowing you to encapsulate all details in one place. Improve your application cache maintanance with less raw strings and static typing.
9
9
10
-
## Support us
10
+
```php
11
+
/**
12
+
* @implements CacheObject<CarbonInterface>
13
+
*
14
+
* @method static self make(User $user)
15
+
*/
16
+
final readonly class SomeUserCache implements CacheObject
public function transformer(): SerializeTransformer
41
+
{
42
+
return new SerializeTransformer();
43
+
}
44
+
}
45
+
```
13
46
14
-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
47
+
## Table of contents
15
48
16
-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
49
+
-[Installation](#installation)
50
+
-[Usage](#usage)
51
+
-[Creating](#creating)
52
+
-[CacheObject](#cacheobject)
53
+
-[CacheKey](#cachekey)
54
+
-[StringKey](#stringkey)
55
+
-[HashedKey](#hashedkey)
56
+
-[Time-to-live](#time-to-live)
57
+
-[CacheValueTransformer](#cachevaluetransformer)
58
+
-[JsonTransformer](#jsontransformer)
59
+
-[SerializeTransformer](#serializetransformer)
60
+
-[EncryptedTransformer](#encryptedtransformer)
61
+
-[GuardTransformer](#guardtransformer)
62
+
-[Traits](#traits)
63
+
-[CacheObjectActions](#cacheobjectactions)
64
+
-[CacheObjectDriver](#cacheobjectdriver)
65
+
-[CacheDriver](#cachedriver)
66
+
-[Events](#events)
67
+
-[CacheObjectStored](#cacheobjectstored)
68
+
-[CacheObjectRetrieved](#cacheobjectretrieved)
69
+
-[CacheObjectMissed](#cacheobjectmissed)
70
+
-[CacheObjectDeleted](#cacheobjectdeleted)
71
+
-[Extending](#extending)
72
+
-[PHPStan](#phpstan)
73
+
-[Testing](#testing)
74
+
-[Changelog](#changelog)
75
+
-[Contributing](#contributing)
76
+
-[Credits](#credits)
77
+
-[Licence](#license)
17
78
18
79
## Installation
19
80
@@ -23,39 +84,213 @@ You can install the package via composer:
23
84
composer require eribloo/laravel-cache-objects
24
85
```
25
86
26
-
You can publish and run the migrations with:
87
+
## Usage
88
+
89
+
### Creating
90
+
91
+
You can create basic Cache Object by running Artisan Command:
Decorator for other keys that returns hashes key before storage. Accepts optional algorithm in constructor (`sha256` by default).
127
+
128
+
```php
129
+
public function key(): EriBloo\CacheObjects\ValueObjects\Keys\HashedKey
130
+
{
131
+
return new HashedKey(
132
+
key: new StringKey('some-cache'),
133
+
algo: 'md5',
134
+
);
135
+
}
50
136
```
51
137
52
-
## Usage
138
+
### Time-to-live
139
+
140
+
Defined with `Carbon\CarbonInterval`. Values that resolve to 0 or less seconds are considered to be stored forever.
141
+
142
+
### CacheValueTransformer
143
+
144
+
Transformers are classes responsible for modifying values before storage and after retrieval.
145
+
146
+
##### JsonTransformer
147
+
148
+
Uses `json_encode` on save and `json_decode` on load. Accepts optional flags and depth in constructor.
149
+
150
+
```php
151
+
public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\JsonTransformer
152
+
{
153
+
return new JsonTransformer(
154
+
loadFlags: JSON_INVALID_UTF8_SUBSTITUTE,
155
+
saveFlags: JSON_UNESCAPED_UNICODE,
156
+
depth: 256,
157
+
);
158
+
}
159
+
```
160
+
161
+
##### SerializeTransformer
162
+
163
+
Transformer that uses PHP `serialize` on save and `unserialize` on load. Accepts optional `class-string[]|bool` in constructor to specify classes allowed for deserialization.
164
+
165
+
```php
166
+
public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\SerializeTransformer
167
+
{
168
+
return new SerializeTransformer(allowedClasses: [SomeClass::class]);
169
+
}
170
+
```
171
+
172
+
##### EncryptedTransformer
173
+
174
+
Decorator for other transformer that uses `Crypt::encryptString` on save and `Crypt::decryptString` on load.
175
+
176
+
```php
177
+
public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\EncryptedTransformer
178
+
{
179
+
return new EncryptedTransformer(
180
+
transformer: new SerializeTransformer,
181
+
);
182
+
}
183
+
```
184
+
185
+
##### GuardTransformer
186
+
187
+
Proxy for other transformer that doesn't modify values, but instead validates them before storage or after retrieval. This class accepts up to 2 closures that should throw an Exception when provided value is invalid.
188
+
189
+
```php
190
+
public function transformer(): EriBloo\CacheObjects\ValueObjects\Values\GuardTransformer
191
+
{
192
+
return new GuardTransformer(
193
+
transformer: new EncryptedTransformer(new SerializeTransformer),
194
+
onSaveGuard: function (CarbonInterface $value) {
195
+
if ($value->isPast()) {
196
+
throw new UnexpectedValueException;
197
+
}
198
+
},
199
+
onLoadGuard: null,
200
+
);
201
+
}
202
+
```
203
+
204
+
### Traits
205
+
206
+
##### CacheObjectActions
207
+
208
+
Optional (but helpful) trait that adds usage methods:
209
+
210
+
```php
211
+
public static function make(): static; // easier creation
212
+
public function store(mixed $value): string; // put into storage, returns key stored in cache
213
+
public function retrieve(): mixed; // get from storage
214
+
public function delete(): bool; // remove from storage
215
+
protected function resolveDriver(): CacheObjectDriver; // resolves to default driver from Service Provider, more below
216
+
```
217
+
218
+
### CacheObjectDriver
219
+
220
+
This interface defines methods used for interacting with storage. Currently 1 class exists.
221
+
222
+
##### CacheDriver
223
+
224
+
This class is a default driver that accepts an instance of `Illuminate\Contracts\Cache\Store`. Binding defined in Service Provider resolves this to your default cache storage.
I created this package with ease of configuration in mind so you can easly create `CacheKey`, `CacheValueTransformer` or `CacheObjectDriver` that will suit your needs. Additionally if you have any ideas of classes that could be incorporated into main package feel free to open an Issue or Pull Request.
289
+
290
+
## PHPStan
291
+
292
+
While I omited most of static typing in examples above for clarity, this package is developed with level 8 of PHPStan and parts of code that are working with `mixed` values (transformers, cache object interface and cache object action) are built with generics.
293
+
59
294
## Testing
60
295
61
296
```bash
@@ -68,15 +303,11 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re
68
303
69
304
## Contributing
70
305
71
-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
72
-
73
-
## Security Vulnerabilities
74
-
75
-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
0 commit comments