2017 © Pedro Peláez
 

library cache_object

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

image

oscarricardosan/cache_object

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

  • Friday, November 10, 2017
  • by oscarricardosan
  • Repository
  • 1 Watchers
  • 0 Stars
  • 40 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 8 % Grown

The README.md

Agnostic php package., (*1)

Small package for handling cache in code block and not having to use session and avoid array_has or the like., (*2)


$productCache= new CacheObject([0=> 'A']); $productCache->set(1, 'B'); if($productCache->exists(0)){ return $productCache->get(0); }

use Oscarricardosan\CacheObject\CacheObject; class Product { /** * @var CacheObject */ protected $productCache; public function __construct() { $this->$productCache= new CacheObject([ 'potato'=> ['name'=> 'Potato', 'value'=> 10] ]); } public function addTomatoToProducts($value) { $productCache->set('tomato', $value); } public function getTomatoProduct() { if(!$productCache->exists('tomato')){ $productCache->set('tomato', ['name'=> 'Tomato default', 'value'=> 7]); } return $productCache->get('tomato'); } } ____________________________________________ $product= new Product(); print_r($product->getTomatoProduct()); //['name'=> 'Tomato default', 'value'=> 7] $product->addTomatoToProducts(['name'=> 'Real Tomato', 'real_value'=> 1200, 'money', 'COP']) print_r($product->getTomatoProduct()); //['name'=> 'Real Tomato', 'real_value'=> 1200, 'money', 'COP']

Method "getOrSet", receives as parameters the key to obtain and if it does not exist it executes the second parameter which must be a function. It works to avoid having to use an "if ($ productCache-> exists ('tomato'))", (*3)



public function getTomatoProduct() { return $productCache->getOrSet('tomato', function(){ return ['name'=> 'Tomato default', 'value'=> 7]; }) }

The Versions

10/11 2017

dev-master

9999999-dev

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

  Sources   Download

MIT

by Avatar oscarricardosan

10/11 2017

v1.1.3

1.1.3.0

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

  Sources   Download

MIT

by Avatar oscarricardosan

16/08 2017

v1.1.2

1.1.2.0

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

  Sources   Download

MIT

by Avatar oscarricardosan

16/08 2017

v1.1.1

1.1.1.0

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

  Sources   Download

MIT

by Avatar oscarricardosan

16/08 2017

v1.1.0

1.1.0.0

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

  Sources   Download

MIT

by Avatar oscarricardosan

16/08 2017

v1.0.0

1.0.0.0

Small package for handling cache in code block and not having to use session and avoid 'array_has' or the like.

  Sources   Download

MIT

by Avatar oscarricardosan