diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.github/workflows/php-package-tests.yml b/.github/workflows/php-package-tests.yml new file mode 100644 index 0000000..e0730c3 --- /dev/null +++ b/.github/workflows/php-package-tests.yml @@ -0,0 +1,27 @@ +name: PHP Package Tests + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['8.1'] + + steps: + - uses: actions/checkout@v2 + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run tests + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43589e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/vendor +composer.phar +composer.lock +.DS_Store +.phpunit.result.cache \ No newline at end of file diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results new file mode 100644 index 0000000..e5f4d45 --- /dev/null +++ b/.phpunit.cache/test-results @@ -0,0 +1 @@ +{"version":1,"defects":{"Tests\\Unit\\CacheTest::testCacheProvider":8},"times":{"Tests\\Unit\\CacheTest::testCacheProvider":0.036,"Tests\\Unit\\CacheTest::testCacheKeyMaker":0.001}} \ No newline at end of file diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..fd50a9d --- /dev/null +++ b/.styleci.yml @@ -0,0 +1,9 @@ +preset: recommended + +risky: false + +linting: true + +finder: + exclude: + - "tests" \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..5e60a67 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,56 @@ +# Contributions are always welcome + +## Quick guide + + * Fork the repo. + * Checkout the branch you want to make changes on: + * Develop branch in 95% of the cases. + * Install the dependencies: `composer install`. + * Create branch such as: `feature-foo` or `fix-bar`. + * Write some awesome code! + * Add some tests, and ensure your code is PSR-2 compliant. + * Submit your Pull Request + +## When opening a pull request +You can do some things to increase the chance that your pull request is accepted the first time: + + * Submit one pull request per fix or feature. + * If your changes are not up to date - rebase your branch on the parent branch. + * Follow the conventions used in the project. + * Remember about tests and documentation. + +## Naming Conventions + + * Use camelCase, not underscores, for variable, function and method names, arguments. + * Use namespaces for all classes. + * Prefix abstract classes with Abstract. + * Suffix interfaces with Interface. + * Suffix traits with Trait. + * Suffix exceptions with Exception. + * Suffix services with Service. + * Use alphanumeric characters and underscores for file names. + +## PHPDoc + +We generally follow the doc standards of Laravel. + +``` +/** + * Register a binding with the container. + * + * @param string|array $abstract + * @param \Closure|string|null $concrete + * @param bool $shared + * @return void + */ +public function bind($abstract, $concrete = null, $shared = false) +{ + // +} +``` + +## Other general standards we follow + * [PSR-1: Basic Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md) + * [PSR-2: Coding Style Guide](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) + * [PSR-4: Autoloading Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) + * [Symfony Coding Standards](http://symfony.com/doc/current/contributing/code/standards.html) diff --git a/GrafiteSupport-banner.png b/GrafiteSupport-banner.png new file mode 100644 index 0000000..da83ba5 Binary files /dev/null and b/GrafiteSupport-banner.png differ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..da9cbb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Matt Lantz + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..231126a --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,10 @@ +# Grafite Support License + +The MIT License (MIT) +Copyright (c) Grafite Inc. + +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: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..252ff13 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +![Grafite Cache](GrafiteCache-banner.png) + +**Cache** - SQLite based cache driver with some fancy extras like tagging and encryption etc. + +[![Build Status](https://github.com/GrafiteInc/Cache/workflows/PHP%20Package%20Tests/badge.svg?branch=main)](https://github.com/GrafiteInc/Cache/actions?query=workflow%3A%22PHP+Package+Tests%22) +[![Maintainability](https://api.codeclimate.com/v1/badges/a90e41bd64d41508ef0e/maintainability)](https://codeclimate.com/github/GrafiteInc/Cache/maintainability) +[![Packagist](https://img.shields.io/packagist/dt/grafite/cache.svg)](https://packagist.org/packages/grafite/cache) +[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://packagist.org/packages/grafite/cache) + +The Cache package an SQLite based cache driver with some fancy extras like tagging and encryption. + +##### Author(s): +* [Matt Lantz](https://github.com/mlantz) ([@mattylantz](http://twitter.com/mattylantz), mattlantz at gmail dot com) + +## Requirements + +1. PHP 8.2+ + +## Compatibility and Support + +| Laravel Version | Package Tag | Supported | +|-----------------|-------------|-----------| +| ^11.x | 1.x | yes | + +### Installation + +Start a new Laravel project: +```php +composer create-project laravel/laravel your-project-name +``` + +Then run the following to add Support +```php +composer require "grafite/cache" +``` + +Append to the `cache.php` config file in the stores array: +```php +'sqlite' => [ + 'driver' => 'sqlite', + 'table' => 'cache', + 'database' => env('CACHE_DATABASE', database_path('cache.sqlite')), + 'prefix' => '', +], +``` + +## Documentation + +[https://docs.grafite.ca/utilities/cache](https://docs.grafite.ca/utilities/cache) + +## License +Support is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) + +### Bug Reporting and Feature Requests +Please add as many details as possible regarding submission of issues and feature requests + +### Disclaimer +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. diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..be33755 --- /dev/null +++ b/changelog.md @@ -0,0 +1,4 @@ +# Change Log - Grafite Cache +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). +---- diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..dc653c5 --- /dev/null +++ b/composer.json @@ -0,0 +1,55 @@ +{ + "name": "grafite/cache", + "description": "An SQLite based cache with some extra features.", + "license": "MIT", + "keywords": [ + "Laravel" + ], + "authors": [ + { + "name": "Matt Lantz", + "email": "mattlantz@gmail.com" + } + ], + "require": { + "php": "^8.2", + "illuminate/support": "^11.0", + "illuminate/collections": "^11.0" + }, + "require-dev": { + "phpunit/phpunit": "^10.0", + "mockery/mockery": "^1.0", + "laravel/pint": "^1.10", + "orchestra/testbench": "^8.0|^9.0" + }, + "autoload": { + "psr-4": { + "Grafite\\Cache\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Grafite\\Cache\\CacheProvider" + ] + } + }, + "minimum-stability": "dev", + "prefer-stable": true, + "scripts": { + "check-style": "vendor/bin/pint --test", + "fix-style": "vendor/bin/pint", + "insights": "vendor/bin/phpinsights", + "test": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover clover.xml && php coverage-checker.php clover.xml 50" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + } +} diff --git a/coverage-checker.php b/coverage-checker.php new file mode 100644 index 0000000..b5a7bab --- /dev/null +++ b/coverage-checker.php @@ -0,0 +1,33 @@ +xpath('//metrics'); +$totalElements = 0; +$checkedElements = 0; + +foreach ($metrics as $metric) { + $totalElements += (int) $metric['elements']; + $checkedElements += (int) $metric['coveredelements']; +} + +$coverage = ($checkedElements / $totalElements) * 100; + +if ($coverage < $percentage) { + echo 'Code coverage is '.$coverage.'%, which is below the accepted '.$percentage.'%'.PHP_EOL; + + exit(1); +} + +echo 'Code coverage is '.$coverage.'% - OK!'.PHP_EOL; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..79d0644 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,19 @@ + + + + + ./tests/Unit/ + + + + + + ./src + + + + + + + + diff --git a/src/CacheProvider.php b/src/CacheProvider.php new file mode 100644 index 0000000..d11a08a --- /dev/null +++ b/src/CacheProvider.php @@ -0,0 +1,118 @@ + 'sqlite', + 'database' => config('cache.stores.sqlite.database'), + 'prefix' => config('cache.stores.sqlite.prefix'), + ]); + + Cache::macro('forgetLike', function ($query) { + app('db')->connection('sqlite_cache')->table('cache')->where('key', 'like', "%{$query}%")->delete(); + + return true; + }); + + Cache::macro('forgetTag', function ($queryTag) { + $items = app('db')->connection('sqlite_cache')->table('cache')->get(); + + $keys = $items->filter(function ($item) use ($queryTag) { + [$key, $tags] = explode(':', $item->key); + + foreach (explode('|', $tags) as $tag) { + if ($queryTag === $tag) { + return $item; + } + } + })->pluck('key'); + + app('db')->connection('sqlite_cache')->table('cache')->whereIn('key', $keys)->delete(); + + return true; + }); + + Cache::macro('forgetTags', function ($queryTag) { + $items = app('db')->connection('sqlite_cache')->table('cache')->get(); + + $keys = $items->filter(function ($item) use ($queryTag) { + [$key, $tags] = explode(':', $item->key); + $tags = explode('|', $tags); + + if (count(array_intersect($queryTag, $tags)) == count($queryTag)){ + return $item; + } + })->pluck('key'); + + app('db')->connection('sqlite_cache')->table('cache')->whereIn('key', $keys)->delete(); + + return true; + }); + + Cache::macro('getTag', function ($queryTag) { + $items = app('db')->connection('sqlite_cache')->table('cache')->get(); + + return $items->filter(function ($item) use ($queryTag) { + [$key, $tags] = explode(':', $item->key); + + foreach (explode('|', $tags) as $tag) { + if ($queryTag === $tag) { + return $item; + } + } + })->map(function ($item) { + return unserialize(decrypt($item->value)); + }); + }); + + Cache::macro('key', function ($key, $tags = []) { + if (str_contains($key, ':')) { + throw new Exception("Cache Key cannot contain ':'", 1); + } + + if (empty($tags)) { + return $key; + } + + $compiledKey = $key . ':' . collect($tags)->implode('|'); + + if (strlen($compiledKey) > 255) { + throw new Exception("Cache Key is too long.", 1); + } + + return $compiledKey; + }); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->commands([ + CreateCacheDatabase::class, + ]); + } +} diff --git a/src/Commands/CreateCacheDatabase.php b/src/Commands/CreateCacheDatabase.php new file mode 100644 index 0000000..7ffa4e8 --- /dev/null +++ b/src/Commands/CreateCacheDatabase.php @@ -0,0 +1,40 @@ +info('Creating cache database...'); + + if (! file_exists(database_path('cache.sqlite'))) { + touch(database_path('cache.sqlite')); + // Set the table + app('db')->connection('sqlite_cache')->statement('CREATE TABLE cache (key STRING PRIMARY KEY, value LONGTEXT, expiration INTEGER)'); + } + + return 0; + } +} diff --git a/src/Stores/SqliteStore.php b/src/Stores/SqliteStore.php new file mode 100644 index 0000000..69132c1 --- /dev/null +++ b/src/Stores/SqliteStore.php @@ -0,0 +1,103 @@ +connection('sqlite_cache'); + + parent::__construct($connection, config('cache.stores.sqlite.table', 'cache'), config('cache.stores.sqlite.prefix', '')); + } + + public function putMany(array $values, $seconds) + { + $serializedValues = []; + + $expiration = $this->getTime() + $seconds; + + foreach ($values as $key => $value) { + $serializedValues[] = [ + 'key' => $this->prefix.$key, + 'value' => encrypt($this->serialize($value)), + 'expiration' => $expiration, + ]; + } + + return $this->table()->upsert($serializedValues, 'key') > 0; + } + + public function many(array $keys) + { + if (count($keys) === 0) { + return []; + } + + $results = array_fill_keys($keys, null); + + // First we will retrieve all of the items from the cache using their keys and + // the prefix value. Then we will need to iterate through each of the items + // and convert them to an object when they are currently in array format. + $values = $this->table() + ->whereIn('key', array_map(function ($key) { + return $this->prefix.$key; + }, $keys)) + ->get() + ->map(function ($value) { + return is_array($value) ? (object) $value : $value; + }); + + $currentTime = $this->currentTime(); + + // If this cache expiration date is past the current time, we will remove this + // item from the cache. Then we will return a null value since the cache is + // expired. We will use "Carbon" to make this comparison with the column. + [$values, $expired] = $values->partition(function ($cache) use ($currentTime) { + return $cache->expiration > $currentTime; + }); + + if ($expired->isNotEmpty()) { + $this->forgetManyIfExpired($expired->pluck('key')->all(), prefixed: true); + } + + return Arr::map($results, function ($value, $key) use ($values) { + if ($cache = $values->firstWhere('key', $this->prefix.$key)) { + return $this->unserialize(decrypt($cache->value)); + } + + return $value; + }); + } + + public function add($key, $value, $seconds) + { + if (! is_null($this->get($key))) { + return false; + } + + $key = $this->prefix.$key; + $value = encrypt($this->serialize($value)); + $expiration = $this->getTime() + $seconds; + + if (! $this->getConnection() instanceof SqlServerConnection) { + return $this->table()->insertOrIgnore(compact('key', 'value', 'expiration')) > 0; + } + + try { + return $this->table()->insert(compact('key', 'value', 'expiration')); + } catch (QueryException) { + // ... + } + + return false; + } +} \ No newline at end of file diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..2d3a5d4 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,44 @@ +set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + + $app['config']->set('cache.stores.sqlite', [ + 'driver' => 'sqlite', + 'table' => 'cache', + 'database' => database_path('cache.sqlite'), + 'prefix' => '', + ]); + + $app->make('Illuminate\Contracts\Http\Kernel'); + } + + protected function getPackageProviders($app) + { + return [ + \Grafite\Cache\CacheProvider::class, + ]; + } + + public function setUp(): void + { + parent::setUp(); + + app('config')->set('cache.default', 'sqlite'); + + $this->withoutMiddleware(); + } +} diff --git a/tests/Unit/CacheTest.php b/tests/Unit/CacheTest.php new file mode 100644 index 0000000..20efb60 --- /dev/null +++ b/tests/Unit/CacheTest.php @@ -0,0 +1,38 @@ +assertEquals('bar', $value); + } + + public function testCacheKeyMaker() + { + // Create the database + Artisan::call('make:cache:database'); + + // Put a value + $key = cache()->key('foo', ['bar']); + + cache()->put($key, 'bar', 60); + + $value = Cache::get($key); + + $this->assertEquals('bar', $value); + } +}