master

laravel/framework

Last updated at: 29/12/2023 09:20

ApcWrapper.php

TLDR

This file contains a class called ApcWrapper in the Illuminate\Cache namespace. The class provides methods to interact with the APC cache, supporting both APC and APCu extensions.

Methods

__construct

This method is the constructor for the ApcWrapper class. It checks if the APCu extension is supported and updates the $apcu property accordingly.

get

This method retrieves an item from the cache. It takes a $key parameter and returns the corresponding value from the cache if it exists. If the value is not found, it returns null.

put

This method stores an item in the cache. It takes the $key, $value, and $seconds parameters. The item will be stored in the cache for the specified number of seconds. It returns an array or boolean value depending on the success of the operation.

increment

This method increments the value of an item in the cache. It takes the $key and $value parameters. It returns an integer or boolean value depending on the success of the operation.

decrement

This method decrements the value of an item in the cache. It takes the $key and $value parameters. It returns an integer or boolean value depending on the success of the operation.

delete

This method removes an item from the cache. It takes the $key parameter and returns a boolean value indicating the success of the operation.

flush

This method removes all items from the cache. It returns a boolean value indicating the success of the operation.

<?php

namespace Illuminate\Cache;

class ApcWrapper
{
    /**
     * Indicates if APCu is supported.
     *
     * @var bool
     */
    protected $apcu = false;

    /**
     * Create a new APC wrapper instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->apcu = function_exists('apcu_fetch');
    }

    /**
     * Get an item from the cache.
     *
     * @param  string  $key
     * @return mixed
     */
    public function get($key)
    {
        $fetchedValue = $this->apcu ? apcu_fetch($key, $success) : apc_fetch($key, $success);

        return $success ? $fetchedValue : null;
    }

    /**
     * Store an item in the cache.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @param  int  $seconds
     * @return array|bool
     */
    public function put($key, $value, $seconds)
    {
        return $this->apcu ? apcu_store($key, $value, $seconds) : apc_store($key, $value, $seconds);
    }

    /**
     * Increment the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return int|bool
     */
    public function increment($key, $value)
    {
        return $this->apcu ? apcu_inc($key, $value) : apc_inc($key, $value);
    }

    /**
     * Decrement the value of an item in the cache.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return int|bool
     */
    public function decrement($key, $value)
    {
        return $this->apcu ? apcu_dec($key, $value) : apc_dec($key, $value);
    }

    /**
     * Remove an item from the cache.
     *
     * @param  string  $key
     * @return bool
     */
    public function delete($key)
    {
        return $this->apcu ? apcu_delete($key) : apc_delete($key);
    }

    /**
     * Remove all items from the cache.
     *
     * @return bool
     */
    public function flush()
    {
        return $this->apcu ? apcu_clear_cache() : apc_clear_cache('user');
    }
}