sectionable.php 2.94 KB
Newer Older
1 2
<?php namespace Laravel\Cache\Drivers;

3
abstract class Sectionable extends Driver {
4 5

	/**
6 7 8 9 10 11 12 13 14 15 16 17 18 19
	 * Indicates that section caching is implicit based on keys.
	 *
	 * @var bool
	 */
	public $implicit = true;

	/**
	 * The implicit section key delimiter.
	 *
	 * @var string
	 */
	public $delimiter = '::';

	/**
20 21 22 23 24 25 26
	 * Retrieve a sectioned item from the cache driver.
	 *
	 * @param  string  $section
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return mixed
	 */
27 28 29 30
	public function get_from_section($section, $key, $default = null)
	{
		return $this->get($this->section_item_key($section, $key), $default);
	}
31 32 33 34 35 36 37 38 39 40

	/**
	 * Write a sectioned item to the cache.
	 *
	 * @param  string  $section
	 * @param  string  $key
	 * @param  mixed   $value
	 * @param  int     $minutes
	 * @return void
	 */
41 42 43 44
	public function put_in_section($section, $key, $value, $minutes)
	{
		$this->put($this->section_item_key($section, $key), $value, $minutes);
	}
45 46 47 48 49 50 51 52 53

	/**
	 * Write a sectioned item to the cache that lasts forever.
	 *
	 * @param  string  $section
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
54 55 56 57
	public function forever_in_section($section, $key, $value)
	{
		return $this->forever($this->section_item_key($section, $key), $value);
	}
58 59 60 61 62 63 64 65

	/**
	 * Get a sectioned item from the cache, or cache and return the default value.
	 *
	 * @param  string  $section
	 * @param  string  $key
	 * @param  mixed   $default
	 * @param  int     $minutes
Sergii Grebeniuk committed
66
	 * @param  string  $function
67 68
	 * @return mixed
	 */
69 70 71 72 73 74
	public function remember_in_section($section, $key, $default, $minutes, $function = 'put')
	{
		$key = $this->section_item_key($section, $key);

		return $this->remember($key, $default, $minutes, $function);
	}
75 76 77 78 79 80 81 82 83

	/**
	 * Get a sectioned item from the cache, or cache the default value forever.
	 *
	 * @param  string  $section
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return mixed
	 */
84 85 86 87
	public function sear_in_section($section, $key, $default)
	{
		return $this->sear($this->section_item_key($section, $key), $default);
	}
88 89 90 91 92 93 94 95

	/**
	 * Delete a sectioned item from the cache.
	 *
	 * @param  string  $section
	 * @param  string  $key
	 * @return void
	 */
96 97 98 99
	public function forget_in_section($section, $key)
	{
		return $this->forget($this->section_item_key($section, $key));
	}
100 101 102 103 104 105 106

	/**
	 * Delete an entire section from the cache.
	 *
	 * @param  string    $section
	 * @return int|bool
	 */
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	abstract public function forget_section($section);

	/**
	 * Indicates if a key is sectionable.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	protected function sectionable($key)
	{
		return $this->implicit and $this->sectioned($key);
	}

	/**
	 * Determine if a key is sectioned.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	protected function sectioned($key)
	{
		return str_contains($key, '::');
	}

	/**
	 * Get the section and key from a sectioned key.
	 *
	 * @param  string  $key
	 * @return array
	 */
	protected function parse($key)
	{
		return explode('::', $key, 2);
	}
141 142

}