Php store cache files

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PSR-16 Simple Cache file-based cache and cache management

License

simplecomplex/php-cache

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Caching of complex variables, and variables which are expensive to generate.
Like configuration, localization and service responses.

CacheBroker decouples code using cache from the actual PSR-16 cache implementation.

Defines three cache class aliases:

  • variable time-to-live (default ttl and set() arg ttl)
  • fixed time-to-live (default ttl, set() arg ttl ignored)
  • persistent (default ttl ‘forever’ and set() arg ttl ignored)

Plus three like the above which allow long keys; length 128 instead of the PSR-16 compliant 64.

Ask CacheBroker for an aliased type of cache instance — do not instantiate a particular cache class.

Extend CacheBroker , if you later want to switch from say file-based to database-based caching.

Dependency injection container ID: cache-broker

Recommendation: access (and thus instantiate) the cache broker via DI container ID ‘cache-broker’.
See SimpleComplex Utils Dependency .

FileCache is a thorough and cautious PSR-16 Simple Cache implementation; file-based.
Coded defensively — key (and other argument) validation.

  • default time-to-live; ignore set() arg ttl option; no time-to-live
  • garbage collection
  • clearing all items or expired items only
  • exporting all items, to JSON
  • building/replacing a cache store during production (using a ‘candidate’ store)
  • CLI interface for clearing items, e.g. via cron
  • concurrency issues (storage-wise only)

Cache management, replacement and backup

Defines two extensions to the PSR-16 CacheInterface, implemented by FileCache .

ManageableCacheInterface

  • is the cache store new or empty?
  • setting default time-to-live; setting ‘ignore’ set() arg ttl
  • clearing and exporting
  • listing all cache stores

BackupCacheInterface

  • backup/restore
  • replacing a store, by building a ‘candidate’ and switching to that when it’s complete
// Bootstrap. Dependency::genericSet('cache-broker', function () < return new \SimpleComplex\Cache\CacheBroker(); >); // . // Use. /** @var \Psr\Container\ContainerInterface $container */ $container = Dependency::container(); /** @var \SimpleComplex\Cache\CacheBroker $cache_broker */ $cache_broker = $container->get('cache-broker'); /** * Create or re-initialize a cache store. * * @var \SimpleComplex\Cache\FileCache $cache_store */ $cache_store = $cache_broker->getStore( 'some-cache-store', CacheBroker::CACHE_VARIABLE_TTL ); unset($cache_broker); /** @var mixed $whatever */ $whatever = $cache_store->get('some-key', 'the default value');
# List all cache commands and their help. php cli.php cache -h # One command's help. php cli.php cache-xxx -h # List existing cache stores. php cli.php cache-list-stores # Display/get value of a cache item. php cli.php cache-get store key # Delete a cache item. php cli.php cache-delete store key # Delete all expired items of one or all cache stores. php cli.php cache-clear-expired # Delete all items of one or all cache stores. php cli.php cache-clear # Backup a cache store. php cli.php cache-backup store # Restore a cache store from backup. php cli.php cache-restore store # Destroy one or all cache stores. php cli.php cache-destroy

Create a ‘private’ files directory alongside the document root dir
and make it writable for the webserver user (www-data or apache).

Like:
/var/www/my-host/ http
/var/www/my-host/ private

On first cache store instantiation, FileCache will create directory
private/ lib/simplecomplex/file-cache

If that directory structure isn’t suitable, do either:

  • supply CacheBroker (or FileCache constructor directly) with a ‘path’ argument
  • extend FileCache and override it’s class constant PATH_DEFAULT

Источник

Simple PHP File Cache

In this article, you will learn about a simple file caching process using the PHP programming language. Cache is important because it improves the efficiency of data retrieval. It is a reserved storage location that collects temporary data to help websites, browsers, and apps load faster.

As we know, PHP is an interpreted programming language, which means the server has to execute the code each time a PHP page is requested. Instead of establishing a server connection every time we request the same page, we can store a copy of that page in an external file. Once this gets stored, we can call it from the stored location. This will reduce the server loading time and load the web page much faster.

Simple PHP File Cache

In this example, we are using the Output Buffer to cache the requested file. For this, first create a folder ‘cache‘ in the same directory where you will place the ‘index.php‘ file. After that, create two files and copy and paste the following code.

1. cache.php

  class CachingClass < var $cache_time = null; var $file_name = null; function __construct( $file_name, $cache_time = 0 ) < $this->cache_time = ( $cache_time > 0 ) ? $cache_time : (300 * 60); $this->file_name = $file_name; > function startBuffering( ) < ob_start(); > function stopBuffering( ) < $fp = fopen($this->file_name, 'w'); fwrite($fp, ob_get_contents()); fclose($fp); ob_end_flush(); > function check() < return(file_exists($this->file_name) && (time() - $this->cache_time < filemtime($this->file_name))); > > ?> 

In the above example-
ob_start()- This function activates output buffering.
ob_get_contents()- This function returns the contents of the output buffer.
ob_end_flush()- Use this function to disable output buffering.

This is the main file, that we will call in the browser. In this file, first we include the ‘cache.php‘, and then we check the already buffered data. If the condition is true, then display the buffered page on the browser, otherwise, start buffering and display the page.

  // File to store cache $cachefile = "cache/storecache.php"; // Include cache class include_once 'cache.php'; $buffer_object = new CachingClass($cachefile); // check already buffered data if( $buffer_object->check() ) < include_once $cachefile; > else < // start buffering $buffer_object->startBuffering(); ?>   Cache this page Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.   > // stop buffering $buffer_object->stopBuffering(); ?> 

Источник

Читайте также:  Show tables mysql php пример
Оцените статью