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 6.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Audit

    • Audit
    • How to install
    • Setup
    • Recording
    • Events
  • Circuits

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

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • 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
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • How to install
    • Setup
    • Usage

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

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

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

    • ETags
    • How to install
    • Setup
    • Usage
    • Generators

      • Default Generator
      • Custom Generator
    • Eloquent Models
    • Macros
  • Events

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

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Flysystem

    • Introduction
    • Database Adapter

      • Introduction
      • How to install
      • Setup
      • Data Deduplication
      • MIME-Type Detection
  • Http

    • Api

      • Http API
      • How to install
      • Setup
      • Resources

        • Introduction
        • Timestamps
        • Self-Link
        • Relations
        • Registrar
      • Middleware

        • Introduction
        • Request Must Be Json
        • Capture Fields To Select
    • 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
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

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

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Maintenance

    • Modes

      • Maintenance Modes
      • How to install
      • Setup
      • Basic Usage
      • Available Drivers
  • Mime Types

    • MIME-Types
    • How to install
    • Setup
    • Usage
    • Drivers

      • Available Drivers
      • File Info
  • Properties

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

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Streams

    • Streams
    • How to install
    • Setup
    • How to use

      • Introduction
      • Open and Close
      • Raw Resource
      • Seeking
      • Reading
      • Writing
      • Size
      • Truncate
      • Flush
      • Hash
      • MIME-Type
      • Output
      • Locking
      • Transactions
      • Meta
      • Misc
  • 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
    • Duration
    • Json
    • Math
    • Memory
    • Method Helper
    • Invoker
    • Populatable
    • String
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
      • Semantic 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 /config/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|null $userTagsCache = null;

    public function setUserTagsCache(Repository|null $repository): static
    {
        $this->userTagsCache = $repository;
        
        return $this;
    }
    
    public function getUserTagsCache(): Repository|null
    {
        if( ! $this->hasUserTagsCache()){
            $this->setUserTagsCache($this->getDefaultUserTagsCache());
        }
        return $this->userTagsCache;
    }
    
    public function hasUserTagsCache(): bool
    {
        return isset($this->userTagsCache);
    }
    
    public function getDefaultUserTagsCache(): Repository|null
    {
        $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: 16/02/2023, 09:10
Contributors: Alin Eugen Deac, alin
Prev
Events
Next
Logging