tempest-framework icon indicating copy to clipboard operation
tempest-framework copied to clipboard

[Feature Request]: Disabled caches

Open brendt opened this issue 1 year ago • 3 comments

Description

Currently there are two env variables DISCOVERY_CACHE and CACHE, and they both work differently:

  • Discovery cache isn't using Cache yet
  • CACHE disabled both GenericCache and ViewCache

I think we need a better approach: a way to toggle individual caches, as well as disable cache altogether via env

Benefits

/

brendt avatar Oct 10 '24 04:10 brendt

@brendt What do you think of this: We add env variables for everything that can be cached, I mean we can group multiple functionalities under one env variable if they are closely related, here's an example (not the full list of env variables);

  1. DISCOVERY_CACHE
  2. ROUTE_CACHE
  3. VIEW_CACHE
  4. .etc We could have a default value for them like false.

Now we can have the following logic, based on an env variable like APP_ENV=local|test|production First we update the env helper to handled callable default:

function env(string $key, mixed $default = null): mixed
{
    $value = getenv($key);

    if ($value === false) {
        // Check if default is a closure and call it if so
        return is_callable($default) ? $default() : $default;
    }

    return match (strtolower($value)) {
        'true' => true,
        'false' => false,
        'null', '' => null,
        default => $value,
    };
}

Add helper function for discovery cache:

function isDiscoveryCacheEnabled(): bool
{
    return env('DISCOVERY_CACHE', function () {
        $appEnv = env('APP_ENV', 'production');
        if ($appEnv === 'production') {
            return true;
        }

        return false;
    });
}

Now for example for discovery cache:

$shouldCacheDiscovery = isDiscoveryCacheEnabled()

This effectively allows one to selectively disable/enable caching for certain parts of the app but also allow for a master switch, so you can easily disable all the caching by setting the APP_ENV to local and enable only the view caching for testing or all kinds of combinations.

Obviously this is just a rough code example, and much cleaner and better api can be built, but I think it outlines the main idea, to have a master switch that controls all but at the same time allow for customization which is useful in certain scenarios like debugging something.

vsergiu93 avatar Oct 11 '24 11:10 vsergiu93

What is the advantage of splitting all these out?

aidan-casey avatar Oct 11 '24 11:10 aidan-casey

Gives you granularity, want to test something as it would be in prod, but disable route caching or view caching, for example yesterday @brendt could have used this to enable all caching by setting APP_ENV=prod and disable only the view cache by setting VIEW_CACHE=false

For the end users this is not gonna be complicated to understand, usually they will just set the APP_ENV to either prod or local depending if is the app runs locally or in prod, and that's it, sure there might some scenarios where someone would want to run the app locally as it would be in prod but selectively disable caching on certain components, this scenario is not that common but still having this possibility would be quite useful.

TLDR:

  • you are able to selectively disable or enable caching per component while debugging/benchmarking
  • end users will rarely go and selectively disable caching per component

Btw this is just and idea :).

vsergiu93 avatar Oct 11 '24 11:10 vsergiu93