国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

最新webman框架中使用thinkorm后開(kāi)啟字段緩存不生效的解決方法

uspear

webman最新版本thinkorm后開(kāi)啟字段緩存不生效的解決方法

Workerman version:4.0.37

1、按照文檔安裝項(xiàng)目

composer create-project workerman/webman

2、先安裝think-cache(一定要先安裝cache再安裝orm)

安裝ThinkCache

composer require psr/container ^1.1.1 webman/think-cache

3、安裝think-orm(安裝完后再修改webman的原文件)

composer -W require psr/container ^1.1.1 webman/think-orm

修改流程

1、確保這里的先后順序

文件路徑:E:\lab\php\webman\webman\config\
截圖

2、修改ThinkOrm.php文件

文件路徑:E:\lab\php\webman\webman\vendor\webman\think-orm\src\ThinkOrm.php
文件改為這樣

<?php
namespace Webman\ThinkOrm;

use Webman\Bootstrap;
use Workerman\Timer;
use think\facade\Db;
use think\facade\Cache;

class ThinkOrm implements Bootstrap
{
    // 進(jìn)程啟動(dòng)時(shí)調(diào)用
    public static function start($worker)
    {
        // 配置
        Db::setConfig(config('thinkorm'));
        // 新增 緩存-uspear@qq.com
        Db::setCache(Cache::store());
        // 維持mysql心跳
        if ($worker) {
            Timer::add(55, function () {
                $connections = config('thinkorm.connections', []);
                foreach ($connections as $key => $item) {
                    if ($item['type'] == 'mysql') {
                        Db::connect($key)->query('select 1');
                    }
                }
            });
        }
    }
}

截圖

3、修改think-orm源文件

文件路徑:E:\lab\php\webman\webman\vendor\topthink\think-orm\src\db\PDOConnection.php

截圖

如果不會(huì)改,我已經(jīng)上傳gitee,可以直接下載使用

https://gitee.com/uspear/webman-thinkorm-cache.git

2931 3 3
3個(gè)評(píng)論

yzh52521

orm 里增加

Db::setCache(Cache::store());

tp cache 使用
topthink/think-cache 這個(gè)擴(kuò)展用 2.0.x-dev

  • uspear 2022-06-09

    think-cache有修復(fù)他的bugs?

  • yzh52521 2022-06-10

    你都有能力改源碼了 還不知道 官方有沒(méi)有改bug?

  • yzh52521 2022-06-10

    官方也是 修復(fù)了 也不發(fā)版本

Tinywan

哪有這樣子玩的呀!

  • uspear 2022-06-09

    大神,請(qǐng)教一下,不知道怎樣改,這樣改了緩存,不受mysql瓶頸的限制,性能提高了2倍以上

  • Tinywan 2022-06-09

    修改源碼就是不可取

  • uspear 2022-06-09

    看來(lái),大佬有更好的方法,坐等更新代碼

  • Tinywan 2022-06-14

    就是直接使用框架的緩存即可,本地已經(jīng)實(shí)現(xiàn)PSR16

    thinkorm.php 配置

    Db::setConfig(config('thinkorm'));
    // 開(kāi)啟字段緩存
    Db::setCache(\support\Cache::instance());

    查詢(xún)緩存

    $res = UserModel::where('id',20220005)
        ->cache('RestyRedis:'.UserModel::getTable())
        ->select();
    var_dump($res->toArray());

    Redis緩存結(jié)果

    截圖

  • Tinywan 2022-06-14

    如果開(kāi)啟字段緩存 'fields_cache' => true, 緩存數(shù)據(jù)表結(jié)果

    截圖

    案例提交參考地址:https://github.com/Tinywan/webman-admin/commit/f616dededcf2dff8818e8f66ab6084722881023e

小陽(yáng)光
//寫(xiě)個(gè)符合psr16的緩存類(lèi)就完事 
//support\Db::setCache(Container::get(RedisAdapter::class));

  <?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2020/9/2
 * Time: 20:47
 */

namespace support\bootstrap\db\dbcache;

use Psr\SimpleCache\CacheInterface;
use support\bootstrap\Redis;

//psr16 規(guī)范
class RedisAdapter implements CacheInterface
{
    /**
     * 前綴
     * @var string
     */
    private $pre = 'think_cache_';

    /**
     * @return string
     */
    public function __construct($pre = '')
    {
        if ($pre) $this->pre = $pre;
        $this->clear();
    }

    /**
     * 生成key
     * @param $key
     * @return string
     */
    private function addPre($key)
    {
        return $this->pre . $key;
    }

    /**
     * @param string $key
     * @param null $default
     * @return mixed|null
     */
    public function get($key, $default = null)
    {
        try {
            $key = $this->addPre($key);
            #$cache = unserialize(Redis::get($key));
            $cache = json_decode(Redis::get($key), true);
            if (!$cache) return $default;
            return $cache;
        } catch (\Throwable $th) {
            return $default;
        }
    }

    /**
     * @param string $key
     * @param mixed $value
     * @param null $ttl
     * @return bool
     */
    public function set($key, $value, $ttl = null)
    {
        $key = $this->addPre($key);
        try {
            if (is_numeric($ttl)) {
                #return Redis::set($key, serialize($value), 'EX', $ttl);
                return Redis::set($key, json_encode($value), 'EX', $ttl);
            } else {
                #return Redis::set($key, serialize($value));
                return Redis::set($key, json_encode($value));
            }
        } catch (\Throwable $th) {
            return false;
        }
    }

    /**
     * @param string $key
     * @return bool
     */
    public function delete($key)
    {
        $key = $this->addPre($key);
        try {
            Redis::del($key);
        } catch (\Throwable $th) {
            return false;
        }
        return true;
    }

    /**
     * @return bool|int
     */
    public function clear()
    {
        try {
            $keys = Redis::keys("{$this->pre}*");
            return Redis::del($keys);
        } catch (\Throwable $th) {
            return false;
        }
    }

    /**
     * @param iterable $keys
     * @param null $default
     * @return \Generator|iterable
     */
    public function getMultiple($keys, $default = null)
    {
        foreach ($keys as $key) {
            yield $key => $this->get($key, $default);
        }
    }

    /**
     * @param iterable $values
     * @param null $ttl
     * @return bool
     */
    public function setMultiple($values, $ttl = null)
    {
        foreach ($values as $key => $value) {
            if (!$this->set($key, $value, $ttl)) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param iterable $keys
     * @return bool
     */
    public function deleteMultiple($keys)
    {
        foreach ($keys as $key) {
            if (!$this->delete($key)) {
                return false;
            }
        }
        return true;
    }

    /**
     * @param string $key
     * @return bool
     */
    public function has($key)
    {
        try {
            $key = $this->addPre($key);
            return boolval(Redis::exists($key));
        } catch (\Throwable $th) {
            return false;
        }
    }

    public function __destruct()
    {
        $this->clear();
    }
}
  • yzh52521 2022-06-13

    webman 有cache 緩存類(lèi) 為什么要再寫(xiě)一份

  • 小陽(yáng)光 2022-06-14

    現(xiàn)在是有了,老版本沒(méi)有

年代過(guò)于久遠(yuǎn),無(wú)法發(fā)表評(píng)論

uspear

220
積分
0
獲贊數(shù)
0
粉絲數(shù)
2022-06-03 加入
??