Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritix committed Dec 18, 2019
2 parents bfffb8c + b260afd commit d5e2a33
Show file tree
Hide file tree
Showing 28 changed files with 299 additions and 211 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/vendor
/build
composer.phar
composer.lock
composer.lock
.phpunit.result.cache
26 changes: 14 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
env:
global:
- CC_TEST_REPORTER_ID=c169da2961829181a4c1c3dbb4e51a4d7f3e0f9f260730145518de6652b37564
- GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)

language: php

services:
- redis-server

php:
- 7.1
- 7.2
- 7.3
- nightly

matrix:
Expand All @@ -12,16 +20,10 @@ matrix:
before_script:
- travis_wait composer self-update
- travis_wait composer install --prefer-source --no-interaction
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build

script:
- vendor/bin/phpunit

after_script:
- vendor/bin/test-reporter

services:
- redis-server

addons:
code_climate:
repo_token: c169da2961829181a4c1c3dbb4e51a4d7f3e0f9f260730145518de6652b37564
- "phpunit --testsuite=unit --coverage-text --coverage-clover build/logs/clover.xml"
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT; fi
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [4.0] - 2019-12-18
### Added
- Added support for Laravel 6.0

## [3.0.4] - 2019-11-22
### Fixed
- Fixed missing table tag for WhereHas/WhereDoesntHave functions
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Matthias Isler
Copyright (c) 2020 Matthias Isler

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
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ For further information on how this library works and how to debug it please hav
- Intelligent cache invalidation with high granularity
- Works with existing code, no changes required after setup
- Possibility to cache only specific models or exclude some models
- Makes use of [Laravel Redis](http://laravel.com/docs/5.7/redis) (supports [clustering](https://laravel.com/docs/5.7/redis#introduction))
- Makes use of [Laravel Redis](https://laravel.com/docs/6.x/redis) (supports [clustering](https://laravel.com/docs/6.x/redis#configuration))

## Version Compatibility

Laravel | PHP | Lada Cache
:---------|:----------|:----------
5.1-5.6 | 5.6.4+ | 2.x
5.7 | 7.1+ | 3.x
5.7-5.8 | 7.1+ | 3.x
6.0+ | 7.2+ | 4.x

## Performance

Expand All @@ -68,7 +69,7 @@ Install, scale up and lean back.

## Why only Redis?

As you may have discovered while looking at the source code, this library is built directly on top of [Laravel Redis](http://laravel.com/docs/5.7/redis) instead of [Laravel Cache](http://laravel.com/docs/5.7/cache), which would make more sense from a general point of view.
As you may have discovered while looking at the source code, this library is built directly on top of [Laravel Redis](https://laravel.com/docs/6.x/redis) instead of [Laravel Cache](https://laravel.com/docs/6.x/cache), which would make more sense from a general point of view.
However, there are several important reasons behind this decision:

- Storage must be in-memory (wouldn't make much sense otherwise)
Expand All @@ -79,32 +80,22 @@ If you still want to use another storage backend, please feel free to contribute

## Requirements

- PHP 7.1+
- PHP 7.2+
- Redis 2+
- Laravel 5.7+
- [Predis](https://github.com/nrk/predis)
- [Phpiredis](https://github.com/nrk/phpiredis) increases cache performance (optional)
- Laravel 6.0+ (for older versions see [Version Compatibility](#version-compatibility))
- [PhpRedis](https://github.com/phpredis/phpredis) increases cache performance (optional)
- [Laravel Debugbar](https://github.com/barryvdh/laravel-debugbar) provides debug information (optional)

## Installation

Lada Cache can be installed via [Composer](http://getcomposer.org) by requiring the
`spiritix/lada-cache` package in your project's `composer.json`.
Lada Cache can be installed via [Composer](http://getcomposer.org) by requiring the `spiritix/lada-cache` package in your project's `composer.json`.
Or simply run this command:

```sh
composer require spiritix/lada-cache
```

Now you must register the service provider when bootstrapping your Laravel application.
Find the `providers` key in your `config/app.php` and register the Lada Cache Service Provider.

```php
'providers' => array(
// ...
Spiritix\LadaCache\LadaCacheServiceProvider::class,
)
```
The Lada Cache service provider will automatically be installed using [Package Discovery](https://laravel.com/docs/6.x/packages#package-discovery).

Finally, all your models must include the `Spiritix\LadaCache\Database\LadaCacheTrait` trait.
It's a good practice to create a base model class which includes the trait and then gets extended by all your models.
Expand All @@ -126,7 +117,7 @@ In the configuration, you will find the possibility to include or exclude specif
Use the following command to publish the ``lada-cache.php``config file to your configuration folder:

```shell
php artisan vendor:publish --provider="Spiritix\LadaCache\LadaCacheServiceProvider" --tag=config
php artisan vendor:publish
```

## Console commands
Expand All @@ -146,8 +137,8 @@ php artisan lada-cache:enable

## Known issues and limitations

- Doesn't work with [raw SQL queries](http://laravel.com/docs/5.7/database#running-queries). This would require an SQL parser to be implemented which is quite hard and very inefficient. As long as you are only using raw queries for reading data, it just won't get cached. Serious issues will only occur if you use raw queries for writing data (which you shouldn't be doing anyway).
- Doesn't work with [multiple connections](https://laravel.com/docs/5.7/database#using-multiple-database-connections) if done like ``DB::connection('foo')``. Instead, specify the ``protected $connection = 'foo';`` property in the relevant models.
- Doesn't work with [raw SQL queries](https://laravel.com/docs/6.x/database#running-queries). This would require an SQL parser to be implemented which is quite hard and very inefficient. As long as you are only using raw queries for reading data, it just won't get cached. Serious issues will only occur if you use raw queries for writing data (which you shouldn't be doing anyway).
- Doesn't work with [multiple connections](https://laravel.com/docs/6.x/database#using-multiple-database-connections) if done like ``DB::connection('foo')``. Instead, specify the ``protected $connection = 'foo';`` property in the relevant models.
- The cache must be truncated manually after migrations are executed.
- Pessimistic locking (sharedLock, lockForUpdate) requires usage of [raw sql queries](https://github.com/spiritix/lada-cache/issues/49).

Expand All @@ -156,7 +147,7 @@ php artisan lada-cache:enable
Contributions in any form are welcome.
Please consider the following guidelines before submitting pull requests:

- **Coding standard** - It's mostly [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) with some differences.
- **Coding standard** - It's [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
- **Add tests!** - Your PR won't be accepted if it doesn't have tests.
- **Create feature branches** - I won't pull from your master branch.

Expand Down
26 changes: 19 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
}
],
"require": {
"php": ">=7.1",
"illuminate/support": "~5.7",
"illuminate/database": "~5.7",
"illuminate/redis": "~5.7",
"predis/predis": "~1.0"
"php": ">=7.2",
"illuminate/support": "^6.0",
"illuminate/database": "^6.0",
"illuminate/redis": "^6.0"
},
"require-dev": {
<<<<<<< HEAD
"phpunit/phpunit": "~7.0",
"codeclimate/php-test-reporter": "dev-master",
"orchestra/testbench": "~3.7",
"laracasts/testdummy": "~2.3",
"mockery/mockery": "^1.0"
=======
"phpunit/phpunit": "^8.0",
"orchestra/testbench": "^4.0",
"mockery/mockery": "^1.2",
"barryvdh/laravel-debugbar": "^3.2"
>>>>>>> origin/74-laravel6-support
},
"suggest": {
"ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol",
"barryvdh/laravel-debugbar": "Provides debug information about the cache"
},
"autoload": {
Expand All @@ -39,5 +44,12 @@
"Spiritix\\LadaCache\\Tests\\Database\\": "database/"
}
},
"minimum-stability": "stable"
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
"Spiritix\\LadaCache\\LadaCacheServiceProvider"
]
}
}
}
48 changes: 28 additions & 20 deletions database/factories/factories.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
<?php

use Faker\Generator as Faker;
use Spiritix\LadaCache\Tests\Database\Models\Car;
use Spiritix\LadaCache\Tests\Database\Models\CarMaterial;
use Spiritix\LadaCache\Tests\Database\Models\Driver;
use Spiritix\LadaCache\Tests\Database\Models\Engine;
use Spiritix\LadaCache\Tests\Database\Models\Material;
use Spiritix\LadaCache\Tests\Database\Models\CarMaterial;

/* @var $factory callable */
/* @var $faker \Laracasts\TestDummy\FakerAdapter */

$factory(Car::class, [
$factory->define(Car::class, function (Faker $faker) {
return [
'name' => $faker->word,
'engine_id' => $faker->randomNumber(8),
'driver_id' => $faker->randomNumber(8),
]);
];
});

$factory(Engine::class, [
'name' => $faker->word,
'car_id' => $faker->randomNumber(8),
]);
$factory->define(Engine::class, function (Faker $faker) {
return [
'name' => $faker->word,
'car_id' => $faker->randomNumber(8),
];
});

$factory(Driver::class, [
'name' => $faker->word,
]);
$factory->define(Driver::class, function (Faker $faker) {
return [
'name' => $faker->word,
];
});

$factory(Material::class, [
'name' => $faker->word,
]);
$factory->define(Material::class, function (Faker $faker) {
return [
'name' => $faker->word,
];
});

$factory(CarMaterial::class, [
'car_id' => $faker->randomNumber(8),
'material_id' => $faker->randomNumber(8),
]);
$factory->define(CarMaterial::class, function (Faker $faker) {
return [
'car_id' => $faker->randomNumber(8),
'material_id' => $faker->randomNumber(8),
];
});
11 changes: 7 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
processIsolation="false"
stopOnFailure="false">
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-clover" target="build/logs/clover.xml" />
</logging>
<testsuites>
<testsuite name="Test suite">
<testsuite name="unit">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
5 changes: 0 additions & 5 deletions provides.json

This file was deleted.

3 changes: 2 additions & 1 deletion src/Spiritix/LadaCache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ public function get($key)
*/
public function flush()
{
$prefix = config('database.redis.options.prefix');
$keys = $this->redis->keys($this->redis->prefix('*'));

foreach ($keys as $key) {
$this->redis->del($key);
$this->redis->del(str_replace($prefix, '', $key));
}
}
}
44 changes: 30 additions & 14 deletions src/Spiritix/LadaCache/Database/Connection/MysqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,31 @@
namespace Spiritix\LadaCache\Database\Connection;

use Spiritix\LadaCache\Database\Connection;
use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Doctrine\DBAL\Driver\PDOMySql\Driver as DoctrineDriver;
use Illuminate\Database\Query\Grammars\MySqlGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
use Illuminate\Database\Schema\MySqlBuilder;
use PDO;

/**
* Overrides Laravel's MySQL connection class.
*
* Contains code smell copy-pasted from Laravel :(
*
* @package Spiritix\LadaCache\Database\Connection
* @author Matthias Isler <[email protected]>
*/
class MysqlConnection extends Connection
{
/**
* Get the default query grammar instance.
*
* @return \Illuminate\Database\Query\Grammars\MySqlGrammar
*/
protected function getDefaultQueryGrammar()
{
return $this->withTablePrefix(new QueryGrammar);
}

/**
* Get a schema builder instance for the connection.
*
Expand All @@ -42,16 +51,6 @@ public function getSchemaBuilder()
return new MySqlBuilder($this);
}

/**
* Get the default query grammar instance.
*
* @return \Illuminate\Database\Query\Grammars\MySqlGrammar
*/
protected function getDefaultQueryGrammar()
{
return $this->withTablePrefix(new QueryGrammar);
}

/**
* Get the default schema grammar instance.
*
Expand Down Expand Up @@ -81,4 +80,21 @@ protected function getDoctrineDriver()
{
return new DoctrineDriver;
}

/**
* Bind values to their parameters in the given statement.
*
* @param \PDOStatement $statement
* @param array $bindings
* @return void
*/
public function bindValues($statement, $bindings)
{
foreach ($bindings as $key => $value) {
$statement->bindValue(
is_string($key) ? $key : $key + 1, $value,
is_int($value) || is_float($value) ? PDO::PARAM_INT : PDO::PARAM_STR
);
}
}
}
Loading

0 comments on commit d5e2a33

Please sign in to comment.