CI3利用MYSQL,文件缓存实现消息队列

发布时间 2023-07-12 17:57:49作者: 哆啦啊梦

1、MYSQL新建一张自动编号的表c_mq,当用户提交表单申请,便向表中插入一条记录,并获取编号,用于缓存文件的命名,保证不会重复

 2、当用户提交表单信息时,将表单值存入缓存文件

 

3、创建监控器方法,读取缓存文件内容,并插入MYSQL表中

代码:

后台:

用户提交表单值,生成缓存文件,插入c_mq记录,获取编号id

 

1 public function tianjia()
2   {
3     $this->db->set('res', 1)->insert('mq');
4     $iid = $this->db->insert_id();
5     if ($iid) {
6       $val = 'ff_' . $iid;
7       $this->cache->file->save($val, $iid, 36000);
8     }
9   }

 

监控并读取缓存文件

 1   public function jiankong()
 2   {
 3     $arr = scandir(APPPATH . 'cache');
 4 
 5     if (isset($arr[2])) {
 6 
 7       $res = $this->cache->file->get($arr[2]);
 8 
 9       if ($res) {
10         $data = ['val' => $res];
11         $rs = $this->db->insert('val', $data);
12 
13         if ($rs) {
14           $this->cache->file->delete($arr[2]);
15           echo 1;
16           exit;
17         } else {
18           p($res);
19         }
20       }
21     }
22   }

 

 

 前端:

监控:

 

 1 <script>
 2         var url = "<?php echo site_url('hehe/jiankong')?>";
 3              
 4         function hehe() {
 5             $.post(url, function (data) {
 6                 if (data == '1')
 7                     hehe();
 8                 else {
 9                     setTimeout(() => {
10                         hehe();
11                     }, 1000);
12                 }
13             }, 'text');
14         }
15 
16         hehe();
17 
18     </script>