| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Module\LCache;
- use UCore\Helper\Logger;
- trait QueueCache
- {
- public function run(): bool
- {
- $indexs = static::getRequiredArgIndex();
- foreach ($indexs as $index){
- if (!isset($this->arg[$index])){
- Logger::error("Cache-error",[$this->arg,$indexs,$index]);
- return false;
- }
- }
- // dump($this->arg);
- $key = self::getKey($this->arg);
- $key2 = $key . '_PD';// 防止重复执行
- $pd = SCache::get($key2);
- if ($pd->isHit()) {
- // 重复执行
- return true;
- }
- $d = static::getNewData($this->arg);
- // dump($key,$d);
- $data = new CacheItem($key, $d, 0, static::getTtl());
- SCache::put($key, $data, null);
- SCache::put($key2, 1, static::getPreventDuplication());
- return true;
- }
- /**
- * 更新数据,同步,强制
- *
- * @param $arg
- * @return void
- */
- static public function updateSync($parameter)
- {
- $key = static::getKey($parameter);
- $d = static::getNewData($parameter);
- $data = new CacheItem($key, $d, time(), static::getTtl());
- SCache::put($key, $data, null);
- return $key;
- }
- /**
- * 获取缓存key
- * @param $parameter
- * @return string
- */
- static protected function getKey($parameter)
- {
- return md5(serialize([ static::class, $parameter ]));
- }
- /**
- * 获取数据
- *
- * @return mixed|null
- */
- static public function getData($parameter=[],$force = false)
- {
- if($force){
- return static::getNewData($parameter);
- }
- $key = static::getKey($parameter);
- $item = SCache::get($key);
- // var_dump($key);
- // 3ee2d37c91b4abcf418bd24c0c0e1dea
- // 3ee2d37c91b4abcf418bd24c0c0e1dea
- // dump($key,$parameter);
- if (!$item->isHit() ) {
- static::jobUpdate($parameter);
- }
- return $item->getValue();
- }
- }
|