GithubHelp home page GithubHelp logo

bnomei / kirby3-php-cachedriver Goto Github PK

View Code? Open in Web Editor NEW
11.0 1.0 0.0 108 KB

PHP based Cache-Driver

Home Page: https://forum.getkirby.com/t/php-cache-driver-it-is-very-fast/23932

License: MIT License

PHP 100.00%
kirby3 kirby3-cms kirby3-plugin php cache include opcache apcu key value

kirby3-php-cachedriver's Introduction

๐Ÿ˜ Kirby3 PHP Cache-Driver

Release Downloads Build Status Coverage Status Maintainability Twitter

Elephant - a highly performant PHP Cache Driver for Kirby 3

Commerical Usage


Support open source!

This plugin is free but if you use it in a commercial project please consider to sponsor me or make a donation.
If my work helped you to make some cash it seems fair to me that I might get a little reward as well, right?

Be kind. Share a little. Thanks.

โ€ Bruno
ย 
M O N E Y
Github sponsor Patreon Buy Me a Coffee Paypal dontation Hire me)

Installation

  • unzip master.zip as folder site/plugins/kirby3-php-cachedriver or
  • git submodule add https://github.com/bnomei/kirby3-php-cachedriver.git site/plugins/kirby3-php-cachedriver or
  • composer require bnomei/kirby3-php-cachedriver

Usage

Cache methods

$cache = \Bnomei\PHPCache::singleton(); // or
$cache = elephant();

$cache->set('key', 'value', $expireInMinutes);
$value = elephant()->get('key', $default);

elephant()->remove('key');
elephant()->flush();

Cache file(s)

All data is written at the end of the php script life-cycle. This does not count against your script execution time but for example when you change value in the cache with each request writing that big file everytime the time might prove inefficient. Further more incremental updates will be written during script execution depending on the mono_dump setting. Additions to the cache will also be each written in temporary files to improve stability of the cache.

Serialization of data

This plugin defaults to a simple serialization logic which is quick but only serializes primitive data types, closures, objects that have a toArray()-method like Kirby\Cms\Field and Kirby\Toolkit\Obj. This should be enough for most usecases. If you need broader support set bnomei.php-cachedriver.serialize to json which will en- and decode your data as json before storing it. That is a bit slower but will ensure your data contains only primitive types without the hassle of serializing it manually before caching it.

OPCache

Make sure OPCache is configured to load the php files from the cache without any delay. Most probably you will have to set these values in your user.ini or something similar! If you do not set these values you might have outdated data being loaded from php files cached by OPCache instead of loading the right ones you want from disk.

opcache.enable=1
opcache.enable_cli=0 # default is 0, leave it like that
opcache.validate_timestamps=1
opcache.revalidate_freq=0 # default is 2, 0 => on every request

Thanks Al for helping me get these config values right.

Benchmark

elephant()->benchmark(1000);
php : 0.03383207321167  
file : 2.3811981678009

ATTENTION: This will create and remove a lot of cache files and php-cache files

No cache when debugging

When Kirbys global debug config is set to true the complete plugin cache will be flushed and no caches will be read. But entries will be created. This will make you live easier โ€“ trust me.

How to use Elephant with Lapse or Boost

You need to set the cache driver for the lapse plugin to php. Please be aware that the mono-mode is not suited for concurrent writes.

site/config/config.php

<?php
return [
    'bnomei.lapse.cache' => ['type' => 'php'],
    'bnomei.boost.cache' => ['type' => 'php'],
    //... other options
];

Setup Content-File Cache

Use Kirby 3 Boost to setup a cache for content files.

Settings

bnomei.php-cachedriver. Default Description
mono_dump 2048 write to cache file every n changes
check_opcache true check OPCache settings
serialize 'primitive' which is fastest or 'json' for less hassle

Disclaimer

This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

kirby3-php-cachedriver's People

Contributors

bnomei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.