PHP code example of limingxinleo / redis-model

1. Go to this page and download the library: Download limingxinleo/redis-model library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

limingxinleo / redis-model example snippets



namespace limx\Tests\App\RedisModel;

use limx\Models\RedisModel\Model;

class BaseModel extends Model
{
    /**
     * @desc   初始化Redis客户端
     * @author limx
     * @param $parameters
     * @param $options
     */
    protected function initRedisClient($parameters, $options)
    {
        if (!isset($parameters['host'])) {
            $parameters['host'] = '127.0.0.1';
        }

        if (!isset($parameters['port'])) {
            $parameters['port'] = 6379;
        }

        if (!isset($parameters['auth'])) {
            $parameters['password'] = null;
        }

        if (!isset($parameters['database'])) {
            $parameters['database'] = 0;
        }

        parent::initRedisClient($parameters, $options);
    }

    /**
     * @desc   获取Redis模型的Key值
     * @author limx
     * @param $id
     * @return mixed
     */
    public function getPrimaryKey($id)
    {
        return parent::getPrimaryKey($id);
    }

    /**
     * @desc   覆盖更新
     * @author limx
     * @param $primaryKey
     * @param $data
     * @return bool
     */
    public function replace($primaryKey, $data, $ttl = null)
    {
        $info = array_intersect_key($data, array_flip((array)$this->fillable));
        $data = array_merge(array_fill_keys($this->fillable, ''), $info);
        return $this->create($primaryKey, $data, $ttl);
    }

    /**
     * @desc   删除
     * @author limx
     * @param string $primaryKey
     * @return bool|int
     */
    public function destroy($primaryKey)
    {
        if (!is_array($primaryKey)) {
            $primaryKey = [$primaryKey];
        }

        return $this->whereIn($this->primaryFieldName, $primaryKey)->delete();
    }

    /**
     * @desc   删除所有
     * @author limx
     * @return bool|int
     */
    public function flushAll()
    {
        return $this->delete();
    }