AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 4.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Origin
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • registerAsApplication()
    • destroy()
  • Core

    • Athenaeum Core Application
    • Prerequisite
    • How to install
    • Integration
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Http

    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Utils

    • Introduction
    • How to install
    • Array
    • Json
    • Math
    • Method Helper
    • Populatable
    • Version
You are viewing documentation for an outdated version. It is no longer supported!

Caching

To make use of Laravel's Cache component, you should spend a bit of time configuring it.

  • Configuration
  • Obtain Store
    • Via $app
    • Via Facade
    • Via Aware-of Helper
    • Create Own Aware-Of Helper

Configuration

In your /configs/cache.php, you will find several cache "stores", each having a driver. Please ensure that your server environment and application supports the drivers, that you wish to make use of. For instance, if you wish to use a "store" that requires Redis as a driver, then you must fulfill it's requirements. See Laravel's Redis package and it's documentation for details.

In the example illustrated below, a "user-tags" store has been added. It relies of the Redis driver, which uses a custom "users" connection.

<?php
return [
    // ... previous not shown ...

    'stores' => [

        'user-tags' => [
            'driver' => 'redis',
            'connection' => 'users',
        ],
    
        // ... other profiles (stores) ...

    ],

    // ... remaining not shown ...    
];

Obtain Store

There are several approaches to obtain the cache repository, with your desired "store". To better understand following approaches, please review the Service Container chapter.

Via $app

Given that you have direct access to your application instance ($app), use the make() method to obtain the cache repository.

<?php
// Cache Repository with "default" store
$cache = $app->make('cache');

$cache->put('foo', 'bar', 5 * 60);
$foo = $cache->get('foo');

If you wish to obtain a cache repository that uses a specific "store", then you need to specify it via the store() method.

<?php
// Cache Repository with "user-tags" store
$cache = $app->make('cache')->store('user-tags');

$cache->put('foo', 'bar', 5 * 60);
$foo = $cache->get('foo');

Via Facade

You can also use Laravel's Cache Facade, to achieve the same result.

<?php
use Illuminate\Support\Facades\Cache;

Cache::put('foo', 'bar', 5 * 60);
$foo = Cache::get('foo');

Use the store() method to obtain a cache repository that uses a specific "store".

<?php
use Illuminate\Support\Facades\Cache;

Cache::store('user-tags')->put('foo', 'bar', 5 * 60);
$foo = Cache::store('user-tags')->get('foo');

Via Aware-of Helper

Lastly, you can also make use of the Cache and CacheFactory Aware-of Helpers.

<?php

use Aedart\Support\Helpers\Cache\CacheTrait;

class UsersController
{
    use CacheTrait;

    public function index()
    {
        // Default "store"
        $cache = $this->getCache();
    
        // ... remaining not shown ...
    }
}

To obtain a specific store, you use the CacheFactory Aware-of helper.

<?php

use Aedart\Support\Helpers\Cache\CacheFactoryTrait;

class UsersController
{
    use CacheFactoryTrait;

    public function index()
    {
        // Cache Repository with "user-tags" store
        $cache = $this->getCacheFactory()->store('user-tags');
    
        // ... remaining not shown ...
    }
}

Create Own Aware-Of Helper

If you have multiple components that depend on a very specific cache "store", then you could make your own Aware-of helper, which automatically returns the desired "store". Consider the following example.

<?php

namespace Acme\Users\Cache;

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Facades\Cache;

trait UserTagsCache
{
    protected ?Repository $userTagsCache = null;

    public function setUserTagsCache(?Repository $repository)
    {
        $this->userTagsCache = $repository;
        
        return $this;
    }
    
    public function getUserTagsCache(): ?Repository
    {
        if( ! $this->hasUserTagsCache()){
            $this->setUserTagsCache($this->getDefaultUserTagsCache());
        }
        return $this->userTagsCache;
    }
    
    public function hasUserTagsCache(): bool
    {
        return isset($this->userTagsCache);
    }
    
    public function getDefaultUserTagsCache(): ?Repository
    {
        $factory = Cache::getFacadeRoot();
        if (isset($factory)) {
            return $factory->store('user-tags');
        }
        
        return null;
    }
} 

Once your Aware-of Helper is completed, you can use it inside all components that depend on it.

<?php

use Acme\Users\Cache\UserTagsCache;

class UsersController
{
    use UserTagsCache;

    public function index()
    {
        // Cache Repository with "user-tags" store
        $cache = $this->getUserTagsCache();
    
        // ... remaining not shown ...
    }
}
Edit page
Last Updated: 08/09/2020, 18:52
Contributors: Alin Eugen Deac
Prev
Events
Next
Logging