Download the PHP package thamtech/yii2-refresh-ahead-cache without Composer

On this page you can find all versions of the php package thamtech/yii2-refresh-ahead-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 yii2-refresh-ahead-cache

Yii2 Refresh-Ahead Cache

Yii2 Refresh-Ahead Cache can decorate Yii2 cache components or other components to implement a refresh-ahead cache strategy.

The Refresh-Ahead cache strategy (also called Read-Ahead) is used to refresh cached data before it expires. By refreshing cached data before it expires (and doing it asynchronously), end-users never have to suffer the delay of the refresh. Furthermore, it can also help avoid a Cache Stampede.

For license information check the LICENSE-file.

Installation

The preferred way to install this extension is through composer.

or add

to the require section of your composer.json file.

Usage

Decorate Cache Component

You can add Refresh-Ahead capability to your application's cache component by attaching the RefreshAheadCacheBehavior. For example, in your application configuration:

There are a number of parameters you can configure if you declare the behavior as a configuration array:

Decorate any Component

By default, RefreshAheadCacheBehavior assumes that its owner (the component it is attached to as a behavior) is a cache component that will be used for both storage of the cached data as well as the storage of the refresh timeout key. However, you can specify which cache components to use in these cases, so you do not have to attach RefreshAheadCacheBehavior to a Cache component. You can attach it to any Yii Component provided that you specify the cache component(s) to use in the behavior's configuration.

If both data values and refresh timeout keys will be stored in the same cache component, you can set the single cache property as a shortcut for setting both dataCache and refreshTimeoutCache. The following configuration is equivalent to the one above:

Drop-In Replacement for getOrSet

RefreshAheadCacheBehavior adds a getRefreshOrSet() method to the cache or any other component it decorates. This method has the same signature as getOrSet(), so you can perform a drop-in replacement where you currently use getOrSet(). For example,

The Refresh Ahead strategy attempts to add() a refresh timeout key in the refresh timeout cache component with a duration shorter than the requested $duration (half of $duration by default). If the add is successful, it means any previous refresh timeout key had expired and the cached data is due for a refresh.

When getRefreshOrSet() is called with a single callable parameter like the examples above, the Refresh Ahead strategy calls the callable, stores the returned value into the data cache component using the specified $duration, and returns that value (into the $data variable in the examples above).

On the other hand, if the attempt to add the refresh timeout key was not successful, it means the key already exists and is not expired, and therefore, no refresh is currently called for. The Refresh Ahead strategy uses $key to look up the cached value in the data cache component and returns it if it finds it. If it doesn't find it in the data cache (perhaps the cache was flushed or the key was evicted), then the callable is invoked to calculate the new value. The new value is set in the data cache component using the specified $duration and the value is returned.

Typical Usage

The usage can be further improved if you can support asynchronous refreshing. In order to do this, we must provide the Refresh Ahead strategy with two callables: one to trigger a refresh asynchronously and one that will refresh the data synchronously and return the result.

The usage is similar to the examples above, except that the second parameter to getRefreshOrSet() will be a GeneratorInterface object or configuration array instead of a single callable. For example,

If you've configured a mutex component in the RefreshAheadCacheBehavior, you can specify a timeout for acquiring a lock using the mutexLockTimeout property:

By configuring a mutex component on the behavior and setting the mutexLockTimeout as a property on the generator, the Refresh Ahead strategy will attempt to acquire a lock to invoke the generate callable. This way, if multiple requests come in around the same time when the value has expired (a Cache Stampede), the process that first acquires the lock will compute the value and store it in cache. The other processes wait for the lock to be released. Once the first process releases the lock, the value has been computed and is in cache, so the other processes will check for it in cache, find it, and return it without having to invoke the generate callable.

If your task queue can run asynchronously, such as in a cron task, you can use the same $generator in a call to generateAndSet() to complete the refresh process and update the cache value in the background. For example,

This will invoke the generate callable (if the item hasn't already been cached by another invocation of generate at the same time), and sets the result in the cache before returning it.

See Also


All versions of yii2-refresh-ahead-cache with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6.0
thamtech/yii2-di Version ~0.1
yiisoft/yii2 Version >=2.0.14 <2.1
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 thamtech/yii2-refresh-ahead-cache contains the following files

Loading the files please wait ....