Download the PHP package mmikkel/cache-flag without Composer

On this page you can find all versions of the php package mmikkel/cache-flag. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package cache-flag

Cache Flag plugin for Craft CMS

Scrutinizer Code Quality

Cache Flag is a Craft CMS plugin that adds an alternative cache invalidation strategy to template caches, using manually defined keywords ("flags").

Why does this plugin exist?

Cache Flag was originally designed to circumvent common performance issues with the native {% cache %} tag's element query based invalidation strategy.

Since Craft 3.5.0, said performance issues have been solved in core, making Cache Flag redundant for its primary use case. If you were previously using Cache Flag only to avoid performance issues with {% cache %}, you probably don't need it anymore!

However, Cache Flag is still a valid alternative to the native {% cache %} tag if you want to

Table of contents

Requirements

This plugin requires Craft CMS 5.0+

Using Cache Flag

Cache Flag adds a new {% cacheflag %} Twig tag to Craft CMS, which works just like the native {% cache %} tag - except that by default, Cache Flag's template caches are "cold" (i.e. Cache Flag will not save element queries for automatic cache invalidation).

For cache invalidation, Cache Flag adds the ability to "flag" template caches and content with keywords ("flags"). Whenever an element is saved, moved or deleted, Cache Flag will automatically invalidate any flagged template caches matching that element's flags.

Here's how it looks in action:

Note that multiple flags are separated using the pipe delimiter (|).

Tip: In addition to the flagged parameter it's also possible to have Cache Flag clear caches automatically in the same way the native {% cache %} tag does, using the new with elements directive.

I'm going to need an example.

Sure. Let's assume you have a section called "News", and there's a cache that you want to invalidate whenever the content in that section changes (i.e. if entries are saved, deleted, changes status etc). First, you add the flag news (or whatever, the flags can be anything, really) to the "News" section in Cache Flag's CP utility:

Then, you add that same news flag to any relevant caches, using the {% cacheflag %} tag and the flagged parameter:

Now, whenever an entry in the "News" section is saved, moved, deleted or changes status, any caches flagged with news will be automatically invalidated.

Dynamic flags

It's possible to flag caches using dynamic flags based on element IDs and/or UIDs. If you wanted to ensure that a cache is invalidated whenever a particular element is edited, moved or deleted, you can do this:

or if you prefer:

All native element types can be used in dynamic flags:

entry:#{entry.id}
asset:#{asset.id}
category:#{category.id}
tag:#{tag.id}
globalSet:#{globalSet.id}
user:#{user.id}

It's also possible to use the element prefix, which works for all element types (including custom/third party ones):

element:#{element.id}
element:#{element.uid}

Of course, it's possible to combine both standard and dynamic cache flags for a single cache:

Arbitrary flags

The flags you add to your {% cachetags %} caches can be literally anything - and they don't have to be added to an element source (or be dynamic).

A good use case for arbitrary flags is when you've got a cache that don't involve any elements, for example if you wanted to cache output dependent on an external API call or something else that is time-consuming to parse on every request, e.g. something like this:

If you use arbitrary flags, keep in mind that there's nothing that will actually invalidate those caches automatically (they'll essentially be cold caches, albeit flagged). Read up on the different options available for invalidating these - and other - flagged caches here.

Collecting element tags for automatic cache invalidation

Since Cache Flag 1.1.0 (Craft 3.5.0-RC1 or later), it's possible to collect element tags (in addition to your own flags) for automatic cache invalidation just like the native {% cache %} tag does.

If you want Cache Flag to collect element tags for automatic cache invalidation, you can add the with elements directive like this:

Note: It's also possible to omit the flagged parameter and only use with elements, but at that point the {% cacheflag %} tag would work identically to the native {% cache %} tag, and you should probably just use the latter.

Cold caches

If both flagged and with elements are omitted from a {% cacheflag %} tag, that cache will be completely "cold", and it will only be invalidated if/when it expires, or if a user manually invalidates it (or clears the entire data cache) via the Control Panel or the Craft CLI (see also invalidating flagged caches):

Tip: If you're upgrading a Craft 2 site that uses the Cold Cache plugin, this is one way to achive the same thing on Craft 3.

Invalidating flagged caches

Cache Flag will automatically invalidate any caches with flags saved to one or multiple element sources defined in Cache Flag's CP utility, and caches using dynamic flags. These caches are invalidated whenever relevant elements are saved, deleted, moved or changes status.

Cold caches and caches using arbitrary flags must be invalidated manually or programmatically (see below).

Manual cache invalidation

Flagged template caches can be manually invalidated by

Additionally, Cache Flag exposes its own cache-flag/caches/invalidate CLI command, that can be used if you want to clear flagged template caches for specific flags (this also works with arbitrary flags):

./craft cache-flag/caches/invalidate news,images,awesome

If you want to clear flagged caches over HTTP there's also a web controller action cache-flag/caches/invalidate which can be hit with a GET or POST request. This controller action will invalidate all flagged template caches, unless a parameter flags (string[]; array of flags) is present in the request.

Finally, flushing the data cache will delete all template caches, including flagged ones.

Programmatic cache invalidation

Additional parameters

Beyond the flagged and with elements parameters, the {% cacheflag %} tag supports all the same parameters as [the native {% cache %} tag[(https://docs.craftcms.com/v3/dev/tags/cache.html#app)].

Project Config and allowAdminChanges

Cache Flag supports Project Config since v. 1.2.0 (Craft 3.5.0 or later only). If you're upgrading from an earlier version of Cache Flag, the relevant .yaml files will be automatically created after upgrading and running migrations.

Events

Cache Flag dispatches two events:

Both events include a parameter flags, which is an array of the flags Cache Flag is invalidating caches for.

Listening to Cache Flag events

Note: Before Cache Flag 1.1.0, the EVENT_AFTER_DELETE_FLAGGED_CACHES (now deprecated in favor of EVENT_AFTER_INVALIDATE_FLAGGED_CACHES) would only be dispatched if caches were actually deleted. In Cache Flag 1.1.0+, the EVENT_AFTER_INVALIDATE_FLAGGED_CACHES event is dispatched regardless of whether any caches were actually cleared.


All versions of cache-flag with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
craftcms/cms Version ^5.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mmikkel/cache-flag contains the following files

Loading the files please wait ....