Skip to content

Commit c5c5b29

Browse files
authored
Add an example of Cart Storage using Caching
Thought my works example of leveraging Laravel Caching (we're using redis) to store cart data rather than cluttering the database! :)
1 parent 0c475c6 commit c5c5b29

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,46 @@ class DBStorage {
986986
}
987987
```
988988

989+
For example you can also leverage Laravel's Caching (redis, memcached, file, dynamo, etc) using the below example,
990+
991+
```
992+
use Illuminate\Support\Facades\Cache;
993+
994+
class CacheStorage {
995+
996+
public function has($key)
997+
{
998+
return Cache::get($key);
999+
}
1000+
1001+
public function get($key)
1002+
{
1003+
if($this->has($key))
1004+
{
1005+
return new CartCollection(Cache::get($key));
1006+
}
1007+
else
1008+
{
1009+
return [];
1010+
}
1011+
}
1012+
1013+
public function put($key, $value)
1014+
{
1015+
if($row = Cache::get($key))
1016+
{
1017+
// update
1018+
$row->cart_data = $value;
1019+
Cache::put($key, $value, now()->addMinutes(3600));
1020+
}
1021+
else
1022+
{
1023+
Cache::put($key, $value, now()->addMinutes(3600));
1024+
}
1025+
}
1026+
}
1027+
```
1028+
9891029
To make this the cart's default storage, let's update the cart's configuration file.
9901030
First, let us publish first the cart config file for us to enable to override it.
9911031
```php artisan vendor:publish --provider="Darryldecode\Cart\CartServiceProvider" --tag="config"```

0 commit comments

Comments
 (0)