Giter Club home page Giter Club logo

svangr's Introduction

Svangr

Programmatic cache for WP. Implements PSR16 cache interface.

Install

Via Composer:

composer require silvanus/svangr

To use autoloading mechanism, you must include vendor/autoload.php file in your code.

Usage.

Currently has one strategy: transients.

Transient

Working with transients in WP can be a bit awkward and lead to lot of boilerplate code. Svangr offers simple api to work with transients.

Set / Get / Delete

<?php

// Transient cache strategy.
use Silvanus\Svangr\Cache\Transient as Cache;

/**
 * Create new cache instance with key.
 * Key is used as a namespace
 * that binds key/value pairs together.
 */
$cache = new Cache('resources');

// Check existance of value in cache.
$cache->has('myStuff'); // true / false.

// Set individual value.
$cache->set('myStuff', myTimeConsumingFunction());

// Set individual value with refresh time -> defaults to 60 minutes if empty.
$cache->set('willBeGoneInFiveMinutes', myFiveMinuteStuff(), 300)

// Get individual value.
$cache->get('myStuff');

// Delete individual value.
$cache->delete('myStuff'):

Working with multiple values

<?php

// Transient cache strategy.
use Silvanus\Svangr\Cache\Transient as Cache;

// Namespaced cache instance.
$cache = new Cache('resources');

// Set many values
$cache->setMultiple( array( 'events' => myFoo(), 'items' => myBar(), 'resources' => myBaz() ) );

// Get many values. Returned as key => value array.
$cache->getMultiple( 'events', 'items', 'resources' );

// Delete multiple values.
$cache->deleteMultiple( array( 'events', 'items', 'resources' ) ):

Clearing all transients

<?php

// Transient cache strategy.
use Silvanus\Svangr\Cache\Transient as Cache;

// Namespaced cache instance.
$cache = new Cache('resources');

/**
 * Clear all cache values in namespace.
 * NOTE: uses WPDB.
 * If your installation has drop-in replacement for transients,
 * clear might not work. Use delete() method then.
 */
$cache->clear();

Whats in the name?

"Svangr" is Old Norse word for "hungry" or "thin". Caches are hungry, and this is implementation is a thin abstraction.

svangr's People

Contributors

stscoundrel avatar

Stargazers

 avatar

Watchers

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