MongolidLaravel\LaravelCacheComponent::put PHP Method

put() public method

Store an item in the cache for a given number of minutes.
public put ( string $key, mixed $value, float $minutes ) : void
$key string Cache key of the item.
$value mixed Value being stored in cache.
$minutes float Cache ttl.
return void
    public function put(string $key, $value, float $minutes)
    {
        if (is_array($value)) {
            foreach ($value as $index => $document) {
                if ($document instanceof stdClass) {
                    $value[$index] = (array) $document;
                }
            }
        }
        $this->laravelCache->put($key, $this->serializer->convert($value), $minutes);
    }

Usage Example

 public function testShouldPut()
 {
     // Set
     $cacheRepo = m::mock(Repository::class);
     $serializer = m::mock(Serializer::class);
     $component = new LaravelCacheComponent($cacheRepo, $serializer);
     $key = 'foo';
     $value = [(object) ['name' => 'batata']];
     // Expectations
     $serializer->shouldReceive('convert')->once()->with([['name' => 'batata']])->andReturn([['name' => 'chips']]);
     $cacheRepo->shouldReceive('put')->once()->with($key, [['name' => 'chips']], 3)->andReturn($value);
     // Assertion
     $component->put($key, $value, 3);
 }
LaravelCacheComponent