Download the PHP package brynforum/api-cache without Composer

On this page you can find all versions of the php package brynforum/api-cache. 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 api-cache

brynforum/api-cache

Latest Version on Packagist Total Downloads

A Flarum extension that adds server-side response caching to the JSON:API. Configure regex-pattern rules with per-rule TTLs to short-circuit expensive endpoints — top-poster widgets, statistics aggregations, public discussion lists — before they touch the database.

Built and used in production by BrynForum.

Why

Flarum re-aggregates SQL on every request to certain API endpoints. The canonical example is /api/users?filter[top_poster]=true — the popular afrux/top-posters-widget hits it on every page load, and the answer changes once an hour, not once a request.

Edge-caching the response at Cloudflare is a workaround at the wrong layer (and on free/pro plans, Set-Cookie blocks caching anyway). The right fix is caching inside Flarum, where the rules can be tuned per-tenant in admin and the cache key can include exactly what matters.

Installation

Then enable API Cache under Admin → Extensions.

The extension creates a brynforum_api_cache_rules table on first enable.

Optional: Redis backend

The extension uses Flarum's default file cache out of the box. To use Redis (recommended for busy forums), set these environment variables on the container:

The extension auto-detects the env vars, pings the host, and uses Redis if reachable — otherwise transparently falls back to the file driver. Check the active backend via the X-BrynForum-Cache-Backend: redis|file response header.

PHP-FPM defaults to clear_env = yes on most distros. Set clear_env = no in your php-fpm.conf so PHP can read the env vars at runtime.

How it works

A PSR-15 middleware sits on Flarum's API stack. For each GET request:

  1. Walks active rules in priority order; checks the path against path_pattern (PCRE regex) and the querystring against query_filter (optional regex). First match wins.
  2. Computes a stable cache key: sha1(method + path + sorted(query) + scope).
  3. On HIT: returns the cached 200 response (with original headers minus Set-Cookie). No handler invocation, no DB queries.
  4. On MISS: runs the handler. If the response is 200, caches it with the rule's TTL.

Configuration

Admin → API Cache. Add rules with:

Field Type Description
Name text Your label.
Path pattern PCRE regex Required. Includes delimiters, e.g. #^/api/users$#.
Query filter PCRE regex Optional. Matched against the raw querystring, e.g. #filter\[top_poster\]=true#. Empty = match any querystring.
TTL (seconds) int How long to keep the cached response (1 – 604800).
Scope public | guest See below.
Priority int Higher = checked first. Tiebreaker when multiple rules match.
Enabled bool Toggle without deleting.
Allow authenticated requests to seed cache bool, default on When off, only responses generated for logged-out visitors are written into the cache. Everyone can still read the cached payload. Use on endpoints whose response varies subtly between guests and admins — e.g. /api/users includes email when the requester is an admin. See "When NOT to cache".

There's a Clear all cache button to invalidate everything in one click.

Scopes

Verifying it works

Every matched response goes out with two debug headers:

Example:

Safety properties

The extension enforces these regardless of how rules are configured:

When NOT to cache

Some endpoints respond differently to different users even at the same URL. Caching them at scope=public will leak one user's data to another. Use scope=guest (or don't cache at all) for:

Endpoint pattern Why it's per-user
/api/users/<id> Profile incl. email/preferences may be visible to the owner only.
/api/users/me Always the current user.
/api/notifications, /api/notifications/<id> Per-user notification feed.
/api/preferences Per-user prefs.
/api/access_tokens The user's API tokens.
/api/discussions (sometimes) The list response is mostly shared, but if a logged-in user is making the request, fields like lastReadPostId and subscription are mixed in. Safe at scope=guest, not at scope=public.
/api/posts/<id>/edits Edit history may include drafts.

Safe at scope=public:

Endpoint pattern Why it's safe
/api/users?filter[top_poster]=true List, no per-user state.
/api/tags Public taxonomy.
/api/statistics Site-wide aggregates.
Custom public endpoints (/api/brynforum/top-posters etc.) If the extension's response doesn't vary by viewer.

If you're unsure, set the rule to scope=guest first and verify the response shape is identical to what a logged-out visitor sees. Promote to scope=public only once you're certain there's no per-user variation.

Example rules

Use case Path pattern Query filter TTL Scope
Top-poster widget #^/api/users$# #filter\[top_poster\]=true# 600 public
Forum index (logged-out) #^/api/discussions$# 60 guest
Tag list #^/api/tags$# 3600 public
Site statistics endpoint #^/api/statistics$# 300 public

Contributing

Issues and PRs welcome. The extension is intentionally small and focused; before opening a large PR, please file an issue to discuss.

License

MIT © BrynForum


All versions of api-cache with dependencies

PHP Build Version
Package Version
Requires flarum/core Version ^1.8.0
predis/predis Version ^2.2
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 brynforum/api-cache contains the following files

Loading the files please wait ...