mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.24k stars 214 forks source link

Cachable not caching my model on Lumen #362

Closed rifqiakrm closed 4 years ago

rifqiakrm commented 4 years ago

Issue

Hey, thank you for creating this library! It helps me a lot in building some of my projects.

But, I am facing some issues after I installed this library on Lumen. It seems like cachable trait not caching my models. I've been trying to add it to another model that I have but no luck. Am I missing configuration or something? Here is my code

<?php

namespace App\Models;

use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Vinkla\Hashids\Facades\Hashids;

class Tag extends Model
{
    use Cachable;
    ....
}

composer.json

{
    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^7.2.5",
        "dusterio/lumen-passport": "^0.2.18",
        "flipbox/lumen-generator": "^6.0",
        "fruitcake/laravel-cors": "^1.0",
        "genealabs/laravel-model-caching": "^0.8.10",
        "guzzlehttp/guzzle": "^6.5",
        "illuminate/redis": "^7.20",
        "laravel/helpers": "^1.2",
        "laravel/lumen-framework": "^7.0",
        "vinkla/hashids": "^8.1"
    },
    "require-dev": {
        "fzaninotto/faker": "^1.9.1",
        "mockery/mockery": "^1.3.1",
        "phpunit/phpunit": "^8.5"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ]
    }
}

bootstrap/app.php

<?php

require_once __DIR__ . '/../vendor/autoload.php';

(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();

date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
 */

$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);

$app->withFacades();

$app->withEloquent();

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
 */

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

/*
|--------------------------------------------------------------------------
| Register Config Files
|--------------------------------------------------------------------------
|
| Now we will register the "app" configuration file. If the file exists in
| your configuration directory it will be loaded; otherwise, we'll load
| the default version. You may register other files below as needed.
|
 */

$app->configure('auth');
$app->configure('cors');
$app->configure('cache');

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
 */

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
    'cors' => Fruitcake\Cors\HandleCors::class,
    'role' => App\Http\Middleware\Role::class,
]);

$app->middleware([
    Fruitcake\Cors\HandleCors::class,
]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
 */

$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
$app->register(Fruitcake\Cors\CorsServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
$app->register(Vinkla\Hashids\HashidsServiceProvider::class);
$app->register(Illuminate\Redis\RedisServiceProvider::class);

if (!class_exists('Redis')) {
    class_alias('Illuminate\Support\Facades\Redis', 'Redis');
}

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
 */

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__ . '/../routes/web.php';
});

return $app;

.env file

CACHE_DRIVER=redis
REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=mypassword
REDIS_PORT=6379

This is what I get on redis-cli

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>

Environment

Lumen Version: 7.0 Laravel Model Caching Package Version: 0.8.10 PHP Version: 7.4.8 Operating System & Version: Ubuntu 18.04.3 LTS

mikebronner commented 4 years ago

@rifqiakrm Thanks for reporting this. By any chance, would you be able to provide a sample repo that replicates this issue? I don't use Lumen, and the unit tests aren't geared toward Lumen, so at the moment I am unable to test it with Lumen.

rifqiakrm commented 4 years ago

@mikebronner Sorry for the late response. Kinda busy at the time. I'll try to create a sample repo you. See you in a bit.

rifqiakrm commented 4 years ago

Hi @mikebronner, I've created a repository for you to test. You can clone it in here https://github.com/rifqiakrm/sample-query-cache-lumen

mikebronner commented 4 years ago

@rifqiakrm Thanks for doing that! I will look into it soon :)

mikebronner commented 4 years ago

@rifqiakrm Got the repo cloned. What do I do to reproduce the issue?

rhzs commented 4 years ago

@mikebronner I have similar issue. I tried to inspect the cache operation using Clockwork, there wasn't any operation to get from Cache.

mikebronner commented 4 years ago

@rhzs Sorry, not sure how to help you there. If you can provide an error, or a point of failure, or a failing test, I'll be happy to help. I'm not seeing anything wrong when running the test suite. I don't test against Lumen, as I don't use it, but it should generally work.

rifqiakrm commented 4 years ago

@mikebronner You just have to migrate and seed the database, run php artisan serve, and then hit localhost:8000/tags and see the keys on redis-cli if it's there or not.

The controller code is on app/Http/Controllers/ExampleController.php and the model is on app/Models/Tag.php

my .env configuration

CACHE_DRIVER=redis
REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=mypassword
REDIS_PORT=6379
mikebronner commented 4 years ago

@rifqiakrm Awesome, thanks for that ... I didn't know what routes you were using etc. Will try that now.

mikebronner commented 4 years ago

@rifqiakrm So I found out what is happening: the config file is not being loaded in your Lumen app. You need to update Lumen to load config files, and if necessary publish the config file from the package to your Lumen app to where it loads config files.