123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace think\cache;
- abstract class Driver
- {
- protected $handler = null;
- protected $options = [];
- protected $tag;
-
- abstract public function has($name);
-
- abstract public function get($name, $default = false);
-
- abstract public function set($name, $value, $expire = null);
-
- abstract public function inc($name, $step = 1);
-
- abstract public function dec($name, $step = 1);
-
- abstract public function rm($name);
-
- abstract public function clear($tag = null);
-
- protected function getCacheKey($name)
- {
- return $this->options['prefix'] . $name;
- }
-
- public function pull($name)
- {
- $result = $this->get($name, false);
- if ($result) {
- $this->rm($name);
- return $result;
- } else {
- return;
- }
- }
-
- public function remember($name, $value, $expire = null)
- {
- if (!$this->has($name)) {
- $time = time();
- while ($time + 5 > time() && $this->has($name . '_lock')) {
-
- usleep(200000);
- }
- try {
-
- $this->set($name . '_lock', true);
- if ($value instanceof \Closure) {
- $value = call_user_func($value);
- }
- $this->set($name, $value, $expire);
-
- $this->rm($name . '_lock');
- } catch (\Exception $e) {
-
- $this->rm($name . '_lock');
- throw $e;
- } catch (\throwable $e) {
- $this->rm($name . '_lock');
- throw $e;
- }
- } else {
- $value = $this->get($name);
- }
- return $value;
- }
-
- public function tag($name, $keys = null, $overlay = false)
- {
- if (is_null($name)) {
- } elseif (is_null($keys)) {
- $this->tag = $name;
- } else {
- $key = 'tag_' . md5($name);
- if (is_string($keys)) {
- $keys = explode(',', $keys);
- }
- $keys = array_map([$this, 'getCacheKey'], $keys);
- if ($overlay) {
- $value = $keys;
- } else {
- $value = array_unique(array_merge($this->getTagItem($name), $keys));
- }
- $this->set($key, implode(',', $value), 0);
- }
- return $this;
- }
-
- protected function setTagItem($name)
- {
- if ($this->tag) {
- $key = 'tag_' . md5($this->tag);
- $this->tag = null;
- if ($this->has($key)) {
- $value = explode(',', $this->get($key));
- $value[] = $name;
- $value = implode(',', array_unique($value));
- } else {
- $value = $name;
- }
- $this->set($key, $value, 0);
- }
- }
-
- protected function getTagItem($tag)
- {
- $key = 'tag_' . md5($tag);
- $value = $this->get($key);
- if ($value) {
- return array_filter(explode(',', $value));
- } else {
- return [];
- }
- }
-
- public function handler()
- {
- return $this->handler;
- }
- }
|