Laravel使用Swoole Table功能

6 篇文章 0 订阅
4 篇文章 0 订阅

下面的代码直接是一个类,控制器直接引用使用。使用前请确认你已经安装swoole服务和redis服务。我的laravel版本是5.5,swoole版本是最新的。我的生产项目中主要只使用了find方法,其他几个方法仅供参考,使用请谨慎~

<?php
/**
 * Created by PhpStorm.
 * User: Aicken.peng
 * Date: 2019/9/11
 * Time: 9:48
 */

namespace Lib;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use SwooleTW\Http\Table\Facades\SwooleTable;

class SwooleTables
{
    public static $id = 0;

    public static function __callStatic($method, $parameters)
    {
        $redis = new Redis();

        try{
            $result = $redis::{$method}(...$parameters);
        }catch (\Exception $exception){
            return [];
        }
        return $result;
    }

    /**
     * 查找swoole表单行数据
     * 表名 @param $tableName
     * 主键key @param $key
     * 字段名 @param $field
     * 有效时间(秒) @param $valid
     * redis方法 @param $redisMethod
     * 需要redis处理的参数 @param $parameters
     * @return mixed
     */
    public static function find($tableName, $key, $valid = 0, $redisMethod = null, $parameters = [])
    {
        // 判断是否找到数据
        if ($swooleData = self::swooleGet($tableName, $key)) {
            // 判断数据是否处于有效期
            if (Arr::get($swooleData, 'invalidTime') > time()) {
                return unserialize(Arr::get($swooleData, 'content'));
            }
        }

        // 是否需要调用回调函数
        if (empty($redisMethod) or empty($parameters))
            return false;

        // 读取redis数据&写入到swoole表
        $result = call_user_func_array([__CLASS__, $redisMethod], $parameters);
        self::swooleSet($tableName,$key, [['content' => serialize($result), 'invalidTime' => time() + $valid]]);
        return $result;
    }

    // 读取或设置 swoole table数据
    public static function callBackFind($tableName, $key, $valid = 0, $function = null, $params = [])
    {
        // 判断是否找到数据
        if ($swooleData = self::swooleGet($tableName, $key)) {
            // 判断数据是否处于有效期
            if (Arr::get($swooleData, 'invalidTime') > time()) {
                return unserialize(Arr::get($swooleData, 'content'));
            }
        }

        $temp = [];
        if(!empty($function)){
            $temp = self::callFunction($function, $params);
        }

        self::swooleSet($tableName, $key, [['content' => serialize($temp), 'invalidTime' => time() + $valid]]);
        return $temp;
    }


    private static function callFunction($function, $params)
    {
        $result = $function($params);
        return $result;
    }

    /**
     * 添加数据到swoole表
     * @param $tableName 表名
     * @param $key 键名
     * @param array or string $data
     */
    public static function swooleSet($tableName, $key, $data)
    {
        $table = SwooleTable::get($tableName);

        // 循环插入内存表
        foreach ($data as $item) {
            if($key){
                $table->set($key, $item);
            }
        }

        return true;
    }

    /**
     * 获取指定swoole
     * @param $tableName swoole表名
     * @param $key 键名
     * @param null $field 单行 字段名
     * @return array 指定行数据
     */
    public static function swooleGet($tableName, $key, $field = null)
    {
        $table = SwooleTable::get($tableName);

        if (empty($table))
            return false;

        // 取出指定行的指定字段
        if ($field) {
            $data = $table->get($key, $field);
            return $data;
        }

        $data = $table->get($key);
        return $data;
    }

    /**
     * 获取指定swoole表中的所有数据
     * @param $tableName swoole表名
     * @return array 结果集
     */
    public static function swooleGetAll($tableName)
    {
        $temp = [];
        $data = SwooleTable::get($tableName);

        if (empty($table))
            return false;

        foreach ($data as $item) {
            $temp[] = $item;
        }

        return $temp;
    }

    /**
     * 指定字段值自增值
     * @param $tableName swoole表名
     * @param $key 键名
     * @param $field 单行 字段名
     * @param int $incrby 自增值
     * @return mixed
     */
    public static function swooleIncrement($tableName, $key, $field, $incrby = 1)
    {
        $table = SwooleTable::get($tableName);

        if (empty($table))
            return false;

        $result = $table->incr($table, $key, $field, $incrby);
        return $result;
    }

    /**
     * 指定字段值自减值
     * @param $tableName swoole表名
     * @param $key 键名
     * @param $field 单行 字段名
     * @param int $incrby 自增值
     * @return mixed
     */
    public static function swooleDecrement($tableName, $key, $field, $incrby = 1)
    {
        $table = SwooleTable::get($tableName);

        if (empty($table))
            return false;

        $result = $table->incr($table, $key, $field, $incrby);
        return $result;
    }

    /**
     * 获取数据表条目数
     * @param $tableName
     * @return int 数据表条目数
     */
    public static function swooleCount($tableName)
    {
        $table = SwooleTable::get($tableName);

        if (empty($table))
            return false;

        return $table->count();
    }

    /**
     * 判断表中的指定key是否存在
     * @param $tableName 表名
     * @param $key 键名
     * @return bool
     */
    public static function swooleExist($tableName, $key)
    {
        $table = SwooleTable::get($tableName);

        if (empty($table))
            return false;

        return $table->exist($key);
    }

    /**
     * 根据key删除swoole表数据
     * @param $tableName 表名
     * @param $key key
     * @return bool
     */
    public static function swooleDelete($tableName, $key)
    {
        $table = SwooleTable::get($tableName);

        if (empty($tableName))
            return false;

        return $table->del($key);
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值