Eloquent Query Cache
Github Repo
3.x
3.x
  • ⚡Introduction
  • 🎉Support
  • Getting Started
    • 🚀Installation
    • 🙌Showcase
  • Caching
    • ⚡Query Caching
    • 👥Relationships Caching
    • 📑Cache Tags
    • 🛑Cache Invalidation
      • 🌍Global Cache Invalidation
      • 🍸Automatic Invalidation
      • 🤔Many-to-Many Automatic Cache Invalidation
  • Advanced
    • ✨Implement Caching
      • 🔑Generating your own key
      • 🎇Implementing cache for other functions than get()
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Advanced
  2. Implement Caching

Generating your own key

This is how the default key generation function looks like:

public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string
{
    $name = $this->connection->getName();

    // Count has no Sql, that's why it can't be used ->toSql()
    if ($method === 'count') {
        return $name.$method.$id.serialize($this->getBindings()).$appends;
    }

    return $name.$method.$id.$this->toSql().serialize($this->getBindings()).$appends;
}

In some cases, like implementing your own Builder for MongoDB for example, you might not want to use the toSql() and use your own method of generating the per-sql key. You can do so by overwriting the MyCustomBuilder class generatePlainCacheKey() with your own one.

It is, however, highly recommended to use most of the variables provided by the function to avoid cache overlapping issues.

class MyCustomBuilder implements QueryCacheModuleInterface
{
    use QueryCacheModule;

    public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string
    {
        $name = $this->connection->getName();

        // Using ->myCustomSqlString() instead of ->toSql()
        return $name.$method.$id.$this->myCustomSqlString().serialize($this->getBindings()).$appends;
    }
}
PreviousImplement CachingNextImplementing cache for other functions than get()

Last updated 3 years ago

Was this helpful?

✨
🔑