Code Monkey home page Code Monkey logo

tiny-cache's Introduction

Tiny cache

Cache post content, translations and nav menu output in persistent object cache.

This MU plugin works well in production providing you understand its source code (133 sloc).

WordPress performance

How to achieve high performance in WordPress?

Item Tool Speedup
Infrastructure CPU, disk, web server, PHP (OPcache) and DNS Overall performance
In-memory object cache Redis, Memcached, APCu options, post, post meta etc.
Server-side functionality plugins
(backup, db cleanup)
Use WP-CLI instead Degrades performance
Theme and plugins Cache-aware ones using object cache or transients
Translations tiny-translation-cache .mo file parsing
Navigation menus tiny-nav-menu-cache wp_nav_menu()
Post content tiny-cache the_content()
Template output tiny-cache get_template_part()
Widgets widget-output-cache plugin dynamic_sidebar()

This list concentrates on WordPress core generating HTML code. Frontend loading (full page cache) is another topic.

Usage

Of course you need persistent object cache. Consider Redis server and wp-redis plugin.

Replace the_content() calls in your theme.

NOTICE Replace only argument-less calls! $more_link_text and $strip_teaser are not supported.

find -type f -name "*.php" | xargs -r -L 1 sed -i -e 's|\bthe_content();|the_content_cached();|g'

No-cache situations

  • wp_suspend_cache_addition( true );
  • define( 'DONOTCACHEPAGE', true );

Prevent missing plugin

Protection against plugin deactivation.

Copy these to your theme's functions.php.

    if ( ! function_exists( 'the_content_cached' ) ) {
        function the_content_cached( $more_link_text = null, $strip_teaser = false ) {
            the_content( $more_link_text, $strip_teaser );
        }
    }
    if ( ! function_exists( 'get_the_content_cached' ) ) {
        function get_the_content_cached( $more_link_text = null, $strip_teaser = false ) {
            return get_the_content( $more_link_text, $strip_teaser );
        }
    }
    if ( ! function_exists( 'get_template_part_cached' ) ) {
        function get_template_part_cached( $slug, $name = null, $version_hash = '' ) {
            get_template_part( $slug, $name );
        }
    }

Little sisters

  1. Tiny navigation menu cache - for nav menu output
  2. Tiny translation cache - for translations (.mo files)

Alternative

https://github.com/Rarst/fragment-cache

tiny-cache's People

Contributors

szepeviktor 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.