Welcome Guest, Not a member yet? Register   Sign In
custom cache direectory per controller?
#1

I am trying to find a way on caching into a specific directory withing the cache directory per controller. Currently there are thousands of cache files in the cache directory for all of my controllers, is there a way of specifying a directory on a per controller basis?
Reply
#2

You need to customize the Cache library and the CodeIgniter class.
Reply
#3

Yes, it is possible to specify a directory within the cache directory on a per-controller basis. To achieve this, you can leverage a caching library or framework that provides flexible cache management options. Since you haven't mentioned the specific technology stack you're using, I'll provide a general approach.

Create a directory structure within the cache directory: Start by organizing your cache files into separate directories per controller. For example, if you have controllers named "ControllerA" and "ControllerB," create directories like "cache/ControllerA" and "cache/ControllerB" within your cache directory.

Configure the cache directory per controller: Modify the caching configuration for each controller to point to the specific directory you want to use. This configuration step will vary depending on the caching library or framework you are using. Look for options that allow you to set the cache directory path or specify a custom cache directory.

Update cache references in your code: Wherever you are currently referencing the cache files, update those references to reflect the new directory structure. Use the appropriate paths relative to the cache directory to ensure that cache files are stored and retrieved correctly for each controller.

By following these steps, you can organize your cache files into separate directories per controller, making it easier to manage and locate specific cached data for each controller in your application. Remember to consult the documentation or resources specific to your chosen caching library or framework for detailed implementation instructions.
Reply
#4

(This post was last modified: 07-17-2023, 03:42 PM by mullernato.)

I solved this problem as follows, and you can also try it or improve upon this suggestion.
PS: "In my case, I use it to separate the cache generated by the database."
  • Create a new service in Config\Services.php:
PHP Code:
/**
 * The cacheDatabase class provides a simple way to store and retrieve
 * cached database cache.
 *
 * @return CacheInterface
 */
public static function cacheDatabase(?Cache $config null, ?string $handler nullbool $getShared false)
{
    // initially unnecessary, but if you need to change, you can do it.
    if ($getShared) {
        return static::getSharedInstance('cache'$config);
    }

    $config ??= new Cache();

    return CacheFactory::getHandler($config$handler);

  • Create a new class that extends the default FileHandler (I created it in "App\Config\Handlers\FileHandlerDatabase"):
PHP Code:
<?php

namespace Config\Handlers;

use 
CodeIgniter\Cache\Exceptions\CacheException;
use 
CodeIgniter\Cache\Handlers\FileHandler;
use 
Config\Cache;

class 
FileHandlerDatabase extends FileHandler
{
    protected $path;

    protected $subPath;

    public function __construct(Cache $config)
    {
        parent::__construct($config);

        $this->path = !empty($config->subPath) ? $this->path $config->subPath $this->path;

        if (! is_really_writable($this->path)) {
            throw CacheException::forUnableToWrite($this->path);
        }
    }

  • Modify the "App\Config\Cache.php" file to use the new handler:
PHP Code:
public string $subPath 'database/'//Add this property and create the folder in writable/cache, as you need.

public array $validHandlers = [
    'dummy'    => DummyHandler::class,
    'file'      => FileHandler::class,
    'memcached' => MemcachedHandler::class,
    'predis'    => PredisHandler::class,
    'redis'    => RedisHandler::class,
    'wincache'  => WincacheHandler::class,
    'filedb'    => FileHandlerDatabase::class, // Add an alias for your new handler
]; 
  • Now you can use the new handler anywhere in your project as follows:
PHP Code:
$this->cache Services::cacheDatabase(null'filedb'); 
You can use save, get, etc., just like with the default FileHandler.

Observations: It worked perfectly for me, but you can improve upon this suggestion or use it in a different way.

I hope this helps you and other people.
Reply
#5

@mullernato I don't know your requirements, but the sample code seems to be no problem, if it meets your requirements.
You created your own cache handler and service, and you use them.
A common way to customize.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB