"Fossies" - the Fresh Open Source Software Archive

Member "framework-8.65.0/src/Illuminate/Cache/ArrayStore.php" (19 Oct 2021, 4661 Bytes) of package /linux/www/laravel-framework-8.65.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ArrayStore.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Illuminate\Cache;
    4 
    5 use Illuminate\Contracts\Cache\LockProvider;
    6 use Illuminate\Support\InteractsWithTime;
    7 
    8 class ArrayStore extends TaggableStore implements LockProvider
    9 {
   10     use InteractsWithTime, RetrievesMultipleKeys;
   11 
   12     /**
   13      * The array of stored values.
   14      *
   15      * @var array
   16      */
   17     protected $storage = [];
   18 
   19     /**
   20      * The array of locks.
   21      *
   22      * @var array
   23      */
   24     public $locks = [];
   25 
   26     /**
   27      * Indicates if values are serialized within the store.
   28      *
   29      * @var bool
   30      */
   31     protected $serializesValues;
   32 
   33     /**
   34      * Create a new Array store.
   35      *
   36      * @param  bool  $serializesValues
   37      * @return void
   38      */
   39     public function __construct($serializesValues = false)
   40     {
   41         $this->serializesValues = $serializesValues;
   42     }
   43 
   44     /**
   45      * Retrieve an item from the cache by key.
   46      *
   47      * @param  string|array  $key
   48      * @return mixed
   49      */
   50     public function get($key)
   51     {
   52         if (! isset($this->storage[$key])) {
   53             return;
   54         }
   55 
   56         $item = $this->storage[$key];
   57 
   58         $expiresAt = $item['expiresAt'] ?? 0;
   59 
   60         if ($expiresAt !== 0 && $this->currentTime() > $expiresAt) {
   61             $this->forget($key);
   62 
   63             return;
   64         }
   65 
   66         return $this->serializesValues ? unserialize($item['value']) : $item['value'];
   67     }
   68 
   69     /**
   70      * Store an item in the cache for a given number of seconds.
   71      *
   72      * @param  string  $key
   73      * @param  mixed  $value
   74      * @param  int  $seconds
   75      * @return bool
   76      */
   77     public function put($key, $value, $seconds)
   78     {
   79         $this->storage[$key] = [
   80             'value' => $this->serializesValues ? serialize($value) : $value,
   81             'expiresAt' => $this->calculateExpiration($seconds),
   82         ];
   83 
   84         return true;
   85     }
   86 
   87     /**
   88      * Increment the value of an item in the cache.
   89      *
   90      * @param  string  $key
   91      * @param  mixed  $value
   92      * @return int
   93      */
   94     public function increment($key, $value = 1)
   95     {
   96         if (! is_null($existing = $this->get($key))) {
   97             return tap(((int) $existing) + $value, function ($incremented) use ($key) {
   98                 $value = $this->serializesValues ? serialize($incremented) : $incremented;
   99 
  100                 $this->storage[$key]['value'] = $value;
  101             });
  102         }
  103 
  104         $this->forever($key, $value);
  105 
  106         return $value;
  107     }
  108 
  109     /**
  110      * Decrement the value of an item in the cache.
  111      *
  112      * @param  string  $key
  113      * @param  mixed  $value
  114      * @return int
  115      */
  116     public function decrement($key, $value = 1)
  117     {
  118         return $this->increment($key, $value * -1);
  119     }
  120 
  121     /**
  122      * Store an item in the cache indefinitely.
  123      *
  124      * @param  string  $key
  125      * @param  mixed  $value
  126      * @return bool
  127      */
  128     public function forever($key, $value)
  129     {
  130         return $this->put($key, $value, 0);
  131     }
  132 
  133     /**
  134      * Remove an item from the cache.
  135      *
  136      * @param  string  $key
  137      * @return bool
  138      */
  139     public function forget($key)
  140     {
  141         if (array_key_exists($key, $this->storage)) {
  142             unset($this->storage[$key]);
  143 
  144             return true;
  145         }
  146 
  147         return false;
  148     }
  149 
  150     /**
  151      * Remove all items from the cache.
  152      *
  153      * @return bool
  154      */
  155     public function flush()
  156     {
  157         $this->storage = [];
  158 
  159         return true;
  160     }
  161 
  162     /**
  163      * Get the cache key prefix.
  164      *
  165      * @return string
  166      */
  167     public function getPrefix()
  168     {
  169         return '';
  170     }
  171 
  172     /**
  173      * Get the expiration time of the key.
  174      *
  175      * @param  int  $seconds
  176      * @return int
  177      */
  178     protected function calculateExpiration($seconds)
  179     {
  180         return $this->toTimestamp($seconds);
  181     }
  182 
  183     /**
  184      * Get the UNIX timestamp for the given number of seconds.
  185      *
  186      * @param  int  $seconds
  187      * @return int
  188      */
  189     protected function toTimestamp($seconds)
  190     {
  191         return $seconds > 0 ? $this->availableAt($seconds) : 0;
  192     }
  193 
  194     /**
  195      * Get a lock instance.
  196      *
  197      * @param  string  $name
  198      * @param  int  $seconds
  199      * @param  string|null  $owner
  200      * @return \Illuminate\Contracts\Cache\Lock
  201      */
  202     public function lock($name, $seconds = 0, $owner = null)
  203     {
  204         return new ArrayLock($this, $name, $seconds, $owner);
  205     }
  206 
  207     /**
  208      * Restore a lock instance using the owner identifier.
  209      *
  210      * @param  string  $name
  211      * @param  string  $owner
  212      * @return \Illuminate\Contracts\Cache\Lock
  213      */
  214     public function restoreLock($name, $owner)
  215     {
  216         return $this->lock($name, 0, $owner);
  217     }
  218 }