GithubHelp home page GithubHelp logo

juliangut / cache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from desarrolla2/cache

0.0 2.0 0.0 359 KB

A simple cache library. Implements different adapters that you can use and change easily by a manager or similar.

Home Page: https://github.com/desarrolla2/Cache

License: MIT License

PHP 100.00%

cache's Introduction

Desarolla2 Cache

A simple cache library, implementing the PSR-16 standard using immutable objects.

life-is-hard-cache-is

Caching is typically used throughout an applicatiton. Immutability ensure that modifying the cache behaviour in one location doesn't result in unexpected behaviour due to changes in unrelated code.

Desarolla2 Cache aims to be the most complete, correct and best performing PSR-16 implementation available.

Latest version Latest version Software License Build Status Coverage Status Quality Score Sensiolabs Insight Total Downloads Today Downloads Gitter

Installation

composer require desarrolla2/cache

Usage

use Desarrolla2\Cache\Memory as Cache;

$cache = new Cache();

$value = $cache->get('key');

if (!isset($value)) {
    $value = do_something(); 
    $cache->set('key', $value, 3600);
}

echo $value;

Adapters

The following implementation allows you to combine cache adapters.

Other implementations are planned. Please vote or provide a PR to speed up the process of adding the to this library.

Options

You can set options for cache using the withOption or withOptions method. Note that all cache objects are immutable, setting an option creates a new object.

TTL

All cache implementations support the ttl option. This sets the default time (in seconds) that cache will survive. It defaults to one hour (3600 seconds).

Setting the TTL to 0 or a negative number, means the cache should live forever.

Methods

Each cache implementation has the following Psr\SimpleCache\CacheInterface methods:

get(string $key [, mixed $default])

Retrieve the value corresponding to a provided key

has(string $key)

Retrieve the if value corresponding to a provided key exist

set(string $key, mixed $value [, int $ttl])

Add a value to the cache under a unique key

delete(string $key)

Delete a value from the cache

clear()

Clear all cache

getMultiple(array $keys)

Obtains multiple cache items by their unique keys

setMultiple(array $values [, int $ttl])

Persists a set of key => value pairs in the cache

deleteMultiple(array $keys)

Deletes multiple cache items in a single operation

.

The Desarrolla2\Cache\CacheInterface also has the following methods:

withOption(string $key, string $value)

Set option for implementation. Creates a new instance.

withOptions(array $options)

Set multiple options for implementation. Creates a new instance.

getOption(string $key)

Get option for implementation.

Packers

Cache objects typically hold a Desarrolla2\Cache\Packer\PackerInterface object. By default, packing is done using serialize and unserialize.

Available packers are:

  • SerializePacker using serialize and unserialize
  • JsonPacker using json_encode and json_decode
  • NopPacker does no packing
  • MongoDBBinaryPacker using serialize and unserialize to store as BSON Binary

PSR-16 incompatible packers

The JsonPacker does not fully comply with PSR-16, as packing and unpacking an object will probably not result in an object of the same class.

The NopPacker is intended when caching string data only (like HTML output) or if the caching backend supports structured data. Using it when storing objects will might give unexpected results.

Contributors

Daniel González Twitter: @desarrolla2
Arnold Daniels Twitter: @ArnoldDaniels

cache's People

Contributors

ajbdev avatar benswinburne avatar bryantaxs avatar desarrolla2 avatar echernyavskiy avatar gresakg avatar gries avatar itn3rd77 avatar jasny avatar lukasz-schab avatar nike-17 avatar ozziest avatar poratuk avatar pranavpc avatar ragboyjr avatar scottrobertson avatar sunspikes 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.