Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
98fe58d
wip
francoism90 Oct 27, 2024
17ae16b
Fix styling
francoism90 Oct 27, 2024
99fc293
wip
francoism90 Oct 27, 2024
1ee171d
Fix styling
francoism90 Oct 27, 2024
f37fa22
wip
francoism90 Oct 27, 2024
44f923e
Fix styling
francoism90 Oct 27, 2024
9e5a976
wip
francoism90 Oct 27, 2024
3a3e0cf
wip
francoism90 Oct 27, 2024
08b220d
Fix styling
francoism90 Oct 27, 2024
9c17c57
wip
francoism90 Oct 27, 2024
f205b38
wip
francoism90 Oct 27, 2024
36f3bd1
wip
francoism90 Oct 27, 2024
8d6afb7
wip
francoism90 Oct 27, 2024
be6f863
wip
francoism90 Oct 27, 2024
b9d546f
Fix styling
francoism90 Oct 27, 2024
ca0df96
wip
francoism90 Oct 27, 2024
a051c52
Fix styling
francoism90 Oct 27, 2024
030de49
wip
francoism90 Oct 27, 2024
5091e64
wip
francoism90 Oct 27, 2024
251f3fa
wip
francoism90 Oct 27, 2024
76eb629
wip
francoism90 Oct 27, 2024
c2f47de
Add redis
francoism90 Oct 27, 2024
9c53ef5
wip
francoism90 Oct 27, 2024
2a8a588
Fix styling
francoism90 Oct 27, 2024
0b7198f
wip
francoism90 Oct 27, 2024
e4219b3
wip
francoism90 Oct 27, 2024
9b71558
wip
francoism90 Oct 27, 2024
dc46a54
wip
francoism90 Oct 27, 2024
926cd3e
wip
francoism90 Oct 27, 2024
c8d18ae
wip
francoism90 Oct 27, 2024
5b8f356
wip
francoism90 Oct 27, 2024
2eaa211
wip
francoism90 Oct 27, 2024
13719e7
wip
francoism90 Oct 27, 2024
9f15e90
wip
francoism90 Oct 27, 2024
b11c971
Update autoload-dev paths in composer.json
francoism90 Oct 27, 2024
4edb744
Add key
francoism90 Oct 27, 2024
90519c8
Fix styling
francoism90 Oct 27, 2024
6704834
Add key to hash
francoism90 Oct 27, 2024
b8c11fa
Fix styling
francoism90 Oct 27, 2024
a7e5ace
wip
francoism90 Oct 27, 2024
4803e01
wip
francoism90 Oct 27, 2024
54cd171
wip
francoism90 Oct 27, 2024
1bc6ea1
wip
francoism90 Oct 27, 2024
57457fe
wip
francoism90 Oct 27, 2024
0f84f78
wip
francoism90 Oct 27, 2024
0b73f49
wip
francoism90 Oct 27, 2024
0a977e4
wip
francoism90 Oct 27, 2024
c3610e8
Fix styling
francoism90 Oct 27, 2024
ba7f87b
wip
francoism90 Oct 27, 2024
b761ed4
wip
francoism90 Oct 27, 2024
ef81e83
wip
francoism90 Oct 27, 2024
28cdca6
Fix styling
francoism90 Oct 27, 2024
b533fb1
wip
francoism90 Oct 27, 2024
2793a50
wip
francoism90 Oct 27, 2024
3362c60
Fix styling
francoism90 Oct 27, 2024
d4c6e0d
Refactor caching methods to include user context
francoism90 Oct 27, 2024
c5fb9ca
wip
francoism90 Oct 27, 2024
b595312
change order
francoism90 Oct 27, 2024
2fad4fd
wip
francoism90 Oct 27, 2024
b719a16
wip
francoism90 Oct 27, 2024
51ec9dd
Refactor namespaces to use Foxws\ModelCache instead of Foxws\UserCache
francoism90 Oct 28, 2024
a06a398
Fix styling
francoism90 Oct 28, 2024
2c5e948
Rename to modelcache
francoism90 Oct 28, 2024
df2d1d5
Add model args
francoism90 Oct 28, 2024
9eab799
Fix styling
francoism90 Oct 28, 2024
5dee052
wip
francoism90 Oct 28, 2024
5df52ff
Add posts table
francoism90 Oct 28, 2024
52c3aa9
wip
francoism90 Oct 28, 2024
addd2a7
wip
francoism90 Oct 28, 2024
5355cc1
wip
francoism90 Oct 28, 2024
21976bd
wip
francoism90 Oct 28, 2024
621b62c
wip
francoism90 Oct 28, 2024
5374e14
Fix styling
francoism90 Oct 28, 2024
e1ff1a7
Add forget test
francoism90 Oct 28, 2024
9f49138
Fix styling
francoism90 Oct 28, 2024
a4fbd63
wip
francoism90 Oct 28, 2024
0f0610a
Use assertFalse(
francoism90 Oct 28, 2024
9d4170d
Add README
francoism90 Oct 28, 2024
42e1340
wip
francoism90 Oct 28, 2024
d32a8ac
wip
francoism90 Oct 28, 2024
a1a7305
wip
francoism90 Oct 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github: :vendor_name
github: Foxws
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/:vendor_slug/:package_name/discussions/new?category=q-a
url: https://github.com/foxws/laravel-modelcache/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/:vendor_slug/:package_name/discussions/new?category=ideas
url: https://github.com/foxws/laravel-modelcache/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a security issue
url: https://github.com/:vendor_slug/:package_name/security/policy
url: https://github.com/foxws/laravel-modelcache/security/policy
about: Learn how to notify us for sensitive bugs
8 changes: 4 additions & 4 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: PHPStan
on:
push:
paths:
- '**.php'
- 'phpstan.neon.dist'
- '.github/workflows/phpstan.yml'
- "**.php"
- "phpstan.neon.dist"
- ".github/workflows/phpstan.yml"

jobs:
phpstan:
Expand All @@ -18,7 +18,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: "8.2"
coverage: none

- name: Install composer dependencies
Expand Down
18 changes: 7 additions & 11 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: run-tests
on:
push:
paths:
- '**.php'
- '.github/workflows/run-tests.yml'
- 'phpunit.xml.dist'
- 'composer.json'
- 'composer.lock'
- "**.php"
- ".github/workflows/run-tests.yml"
- "phpunit.xml.dist"
- "composer.json"
- "composer.lock"

jobs:
test:
Expand All @@ -16,17 +16,13 @@ jobs:
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
os: [ubuntu-latest]
php: [8.3, 8.2]
laravel: [11.*, 10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 11.*
testbench: 9.*
carbon: ^2.63
- laravel: 10.*
testbench: 8.*
carbon: ^2.63
carbon: ^3.1.1

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Changelog

All notable changes to `:package_name` will be documented in this file.
All notable changes to `laravel-modelcache` will be documented in this file.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) :vendor_name <author@domain.com>
Copyright (c) Foxws <francoism90@users.noreply.github.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
91 changes: 50 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,78 @@
# :package_description
# Simple Laravel caching trait for user data

[![Latest Version on Packagist](https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug)
<!--delete-->
---
This repo can be used to scaffold a Laravel package. Follow these steps to get started:
[![Latest Version on Packagist](https://img.shields.io/packagist/v/foxws/laravel-modelcache.svg?style=flat-square)](https://packagist.org/packages/foxws/laravel-modelcache)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/foxws/laravel-modelcache/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/foxws/laravel-modelcache/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/foxws/laravel-modelcache/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/foxws/laravel-modelcache/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/foxws/laravel-modelcache.svg?style=flat-square)](https://packagist.org/packages/foxws/laravel-modelcache)

1. Press the "Use this template" button at the top of this repo to create a new repo with the contents of this skeleton.
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files.
3. Have fun creating your package.
4. If you need help creating a package, consider picking up our <a href="https://laravelpackage.training">Laravel Package Training</a> video course.
---
<!--/delete-->
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/:package_name.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/:package_name)

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).

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).
This package does not cache models, it gives you helpers to populate the Laravel Cache using a model. By default, logged in users will each have their own separate cache.

## Installation

You can install the package via composer:

```bash
composer require :vendor_slug/:package_slug
composer require foxws/laravel-modelcache
```

You can publish and run the migrations with:
You can publish the config file with:

```bash
php artisan vendor:publish --tag=":package_slug-migrations"
php artisan migrate
php artisan vendor:publish --tag="modelcache-config"
```

You can publish the config file with:
## Usage

Implement the `Foxws\ModelCache\Concerns\InteractsWithModelCache` trait to your Eloquent models:

```php
use Foxws\ModelCache\Concerns\InteractsWithModelCache;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use InteractsWithModelCache;
}

```bash
php artisan vendor:publish --tag=":package_slug-config"
```

This is the contents of the published config file:
```php
use Foxws\ModelCache\Concerns\InteractsWithModelCache;
use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
use InteractsWithModelCache;
}

```

To cache a model value:

```php
return [
];
User::first()->modelCache('randomSeed', 0.5);
Video::first()->modelCache('currentTime', 20, now()->addDay()); // cache for one day
```

Optionally, you can publish the views using
To get a cached value:

```bash
php artisan vendor:publish --tag=":package_slug-views"
```php
User::first()->modelCached('randomSeed');
Video::first()->modelCached('currentTime', $default); // with fallback
```

## Usage
To forget a value:

```php
$variable = new VendorName\Skeleton();
echo $variable->echoPhrase('Hello, VendorName!');
Video::first()->modelCacheForget('currentTime');
```

### Creating a custom cache profile

To determine which values should be cached, and for how long, a cache profile class is used. The default class that handles these questions is `Foxws\ModelCache\CacheProfiles\CacheAllSuccessful`.

You can create your own cache profile class by implementing the `Foxws\ModelCache\CacheProfile\CacheProfile`, and overruling the `cache_profile` in `config/modelcache.php`.

## Testing

```bash
Expand All @@ -85,8 +93,9 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [:author_name](https://github.com/:author_username)
- [All Contributors](../../contributors)
This package is entirely based on the [space/laravel-responsecache](https://github.com/spatie/laravel-responsecache/) package.

Please consider to sponsor Spatie, such as purchasing their excellent courses. :)

## License

Expand Down
37 changes: 22 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
{
"name": ":vendor_slug/:package_slug",
"description": ":package_description",
"name": "foxws/laravel-modelcache",
"description": "Cache helper for auth models",
"keywords": [
":vendor_name",
"foxws",
"laravel",
":package_slug"
"laravel-modelcache",
"cache",
"auth"
],
"homepage": "https://github.com/:vendor_slug/:package_slug",
"homepage": "https://github.com/foxws/laravel-modelcache",
"license": "MIT",
"authors": [
{
"name": ":author_name",
"email": "author@domain.com",
"name": "francoism90",
"email": "francoism90@users.noreply.github.com",
"role": "Developer"
}
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.16",
"illuminate/contracts": "^10.0||^11.0"
"illuminate/cache": "^10.0|^11.0",
"illuminate/console": "^10.0|^11.0",
"illuminate/container": "^10.0|^11.0",
"illuminate/contracts": "^10.0||^11.0",
"illuminate/support": "^10.0|^11.0",
"nesbot/carbon": "^2.63|^3.0",
"spatie/laravel-package-tools": "^1.16"
},
"require-dev": {
"laravel/pint": "^1.14",
Expand All @@ -35,19 +42,19 @@
},
"autoload": {
"psr-4": {
"VendorName\\Skeleton\\": "src/",
"VendorName\\Skeleton\\Database\\Factories\\": "database/factories/"
"Foxws\\ModelCache\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"VendorName\\Skeleton\\Tests\\": "tests/",
"Foxws\\ModelCache\\Tests\\": "tests/src/",
"Foxws\\ModelCache\\Tests\\Database\\Factories\\": "tests/database/factories/",
"Workbench\\App\\": "workbench/app/"
}
},
"scripts": {
"post-autoload-dump": "@composer run prepare",
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"clear": "@php vendor/bin/testbench package:purge-laravel-modelcache --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": [
"@composer run prepare",
Expand All @@ -73,10 +80,10 @@
"extra": {
"laravel": {
"providers": [
"VendorName\\Skeleton\\SkeletonServiceProvider"
"Foxws\\ModelCache\\ModelCacheServiceProvider"
],
"aliases": {
"Skeleton": "VendorName\\Skeleton\\Facades\\Skeleton"
"ModelCache": "Foxws\\ModelCache\\Facades\\ModelCache"
}
}
},
Expand Down
38 changes: 38 additions & 0 deletions config/modelcache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

return [
/*
* Determine if the user cache should be enabled.
*/
'enabled' => env('USER_CACHE_ENABLED', true),

/*
* The given class will determinate if a request should be cached. The
* default class will cache all successful GET-requests.
*
* You can provide your own class given that it implements the
* CacheProfile interface.
*/
'cache_profile' => \Foxws\ModelCache\CacheProfiles\CacheAllSuccessful::class,

/*
* This must be the name of any store that is configured in config/cache.php.
*/
'cache_store' => env('USER_CACHE_STORE') ?: env('CACHE_STORE', 'database'),

/*
* Default time-to-live for cache items in seconds.
*/
'cache_lifetime_in_seconds' => (int) env('USER_CACHE_LIFETIME', 60 * 60 * 24 * 7),

/*
* This class is responsible for generating a hash for a request. This hash
* is used to look up a cached response.
*/
'hasher' => \Foxws\ModelCache\Hasher\DefaultHasher::class,

/*
* This class is responsible for serializing responses.
*/
'serializer' => \Foxws\ModelCache\Serializers\DefaultSerializer::class,
];
6 changes: 0 additions & 6 deletions config/skeleton.php

This file was deleted.

Loading