利用MYSQL+redis模拟消息队列

1、插入数据库,获取自动编号,作为redis键名:

function tianjia()
    {
        $model = model(MqModel::class);
        $data = ['rs' => 1];
        $model->insert($data);              //插入
        $id = $model->getInsertID();
        pp($id);
        $key = 'mq_' . $id;
        cache()->save($key, $id, 86400);
    }

2、读取redis值,插入数据库

function add()
    {
        $model = model(MqModel::class);
        $m_val = model(ValModel::class);
        $rs = $model->getdata();
        $nm = $rs['id'];

        while ($nm > 0) {
            $key = 'mq_' . $nm;
            $val = cache()->get($key);

            if ($val) {
                $data = ['val' => $val];
                $r = $m_val->insert($data);

                if ($r) {
                    cache()->delete($key);
                }
            }
            $nm--;
        }
    }

 

前端:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document Title</title>
    <script src="./static/js/jquery.min.js"></script>
</head>

<body>
    <button class="dianji">点击</button>
</body>
<script>
    $.ajaxSettings.async = false;
    $('.dianji').click(function() {
        var url = "/tj";
        for (i = 1; i <= 5000; i++) {
            $.get(url, function(data) {
                console.log(data);
            })
        }
    })
</script>

</html>

监控

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document Title</title>
    <script src="./static/js/jquery.min.js"></script>
</head>

<body>
    <button class="dianji">点击</button>
</body>
<script>
    function listen() {
        $.ajaxSettings.async = false;
        var url = "/mq/add";
        $.get(url, function(data) {
            setTimeout(() => {
                listen();
            }, 200);

        }).fail(function() {
            setTimeout(() => {
                listen();
            }, 200);
        })
    }

    listen();
</script>

</html>

 

posted @ 2024-02-01 12:47  哆啦啊梦  阅读(9)  评论(0编辑  收藏  举报