mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

More possibilities for customisations #177

Closed getriebesand closed 5 years ago

getriebesand commented 6 years ago

Is your feature request related to a problem? Please describe. I'm always frustrated when I try to cusomize the behavior of your implementation. e.G. customize the cache key. Models use the Caching trait with a method called makeCacheKey. Thats fine. And i can overwrite that method in my model. But than I realized that the CachedBuilder doesnt use the method of the Model but also use the Caching trait. Why?

Describe the solution you'd like I want more control over the behavior. CachedBuilder should use makeCacheKey of the model. And you should use the laravel container for binding your classes like CacheKey and CacheTags. So someone can easily hook into the system by replacing them with an own implementation.

mikebronner commented 6 years ago

Hi @getriebesand, thanks for sharing your ideas. At this point this is probably not something I will look into, because of the complexities involved and my limited time.

Can you help me understand why you have to make changes to how the cache keys are generated? Perhaps a better understanding on my part will help me move forward with this. Thanks!

getriebesand commented 6 years ago

I have 2 projects with same database. In future I will move the models in a separate package which can be required by the 2 project. but this will take some time and in the meantime i want to change in one of the projects the cache key (model class name) to match with the other project.

mikebronner commented 6 years ago

Hi @getriebesand, this shouldn't be an issue if you name your database and connection the same in both projects. As you can see that is what is used to create the cache key prefix. Would making the changes to your config/database.php work for you?

    protected function getCachePrefix() : string
    {
        return "genealabs:laravel-model-caching:"
            . $this->getDatabaseConnectionName() . ":"
            . $this->getDatabaseName() . ":"
            . (config("laravel-model-caching.cache-prefix")
                ? config("laravel-model-caching.cache-prefix", "") . ":"
                : "");
    }

If not, can you give me an example of a cache key for the same model from each project so I can compare how they are different? Thanks!

mikebronner commented 5 years ago

You can try overriding getCachePrefix() in the model. That should let you customize as needed. Closing for now.

getriebesand commented 5 years ago

getCachePrefix() doesn't work because the namespace of the models differs in both projects. In CacheKey.php you insert $this->getModelSlug() into the key. And this uses str_slug(get_class($this->model)) which returns the classname with namespace. I need now to extend CacheKey and modify the generation of the key (or getModelSlug()) but I cant replace CacheKey with my own.

Better would be if you place the method getModelSlug() into the models and use $this->model->getModelSlug()