栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php函数、队列等

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php函数、队列等

1.压缩函数(需在php.ini中开启extension=php_zip.dll)

gzencode、gzdeflate和gzcompress

gzencode 默认使用ZLIB_ENCODING_GZIP编码,使用gzip压缩格式,实际上是使用defalte 算法压缩数据,然后加上文件头和adler32校验

gzdeflate 默认使用ZLIB_ENCODING_RAW编码方式,使用deflate数据压缩算法,实际上是先用 LZ77 压缩,然后用霍夫曼编码压缩

gzcompress ;默认使用ZLIB_ENCODING_DEFLATE编码,使用zlib压缩格式,实际上是用 deflate 压缩数据,然后加上 zlib 头和 CRC 校验

这三个函数的比较实质上是三种压缩方法:deflate, zlib, gzip的比较。
从性能的维度看:deflate 好于 gzip 好于 zlib
从文本文件默认压缩率压缩后体积的维度看:deflate 好于 zlib 好于 gzip

举例:

$string = "aaaaaaaaaaaaabbbbbbbbbcccccccddddddddddddeeeeeeeeeeeee啊啊啊啊啊啊啊啊啊啊啊测试测试测试测试长度长度长度";
echo "字符串长度:";
echo strlen($string);
echo "
gzcompress压缩后长度 :";
echo strlen(gzcompress($string,9));
echo "
gzencode压缩后长度:";
echo strlen(gzencode($string,9));
echo "
gzdeflate压缩后长度:";
echo strlen(gzdeflate($string,9));

其中9指的是压缩程度,从0-9

结果:

字符串长度:129
gzcompress压缩后长度 :43
gzencode压缩后长度:55
gzdeflate压缩后长度:37

压缩效果进行排序:gzdeflate、gzcompress、gzencode

与gzcompress想对应的就是gzuncompress解压函数


2.php+redis 消息队列(此部分转载,原文地址http://www.cnblogs.com/lisqiong/p/6039460.html)

 把瞬间服务器的请求处理换成异步操作,缓解服务器压力

 实现数据顺序排列获取

redis实现消息队列步骤如下:

1)redis函数rpush,lpop

2)创建定时任务入队列

3)创建定时任务出队列


private $redis;

    
    public function __construct($host = '10.102.1.8', $port = 6379) {        $this->redis = new Redis();        $this->redis->connect($host, $port);        return $this->redis;
    }

    
    public function set($key, $value, $timeOut=0) {        $retRes = $this->redis->set($key, $value);        if ($timeOut > 0)            $redis->expire('$key', $timeOut);        return $retRes;
    }

    
    public function sadd($key,$value){        return $this->redis->sadd($key,$value);
    }
    
    
    public function zadd($key,$value){        return $this->redis->zadd($key,$value);
    }
    
    
    public function smembers($setName){        return $this->redis->smembers($setName);
    }

    
    public function lpush($key,$value){
        echo "$key - $value n";        return $this->redis->LPUSH($key,$value);
    }
    
      
    public function rpush($key,$value){
        echo "$key - $value n";        return $this->redis->rpush($key,$value);
    }
    
    public function lranges($key,$head,$tail){        return $this->redis->lrange($key,$head,$tail);
    }
    
    
    public function hset($tableName,$field,$value){        return $this->redis->hset($tableName,$field,$value);
    }
    
    public function hget($tableName,$field){        return $this->redis->hget($tableName,$field);
    }
    
    
    
    public function sets($keyArray, $timeout) {        if (is_array($keyArray)) {            $retRes = $this->redis->mset($keyArray);            if ($timeout > 0) {                foreach ($keyArray as $key => $value) {                    $this->redis->expire($key, $timeout);
                }
            }            return $retRes;
        } else {            return "Call  " . __FUNCTION__ . " method  parameter  Error !";
        }
    }

    
    public function get($key) {        $result = $this->redis->get($key);        return $result;
    }

    
    public function gets($keyArray) {        if (is_array($keyArray)) {            return $this->redis->mget($keyArray);
        } else {            return "Call  " . __FUNCTION__ . " method  parameter  Error !";
        }
    }

    
    public function keyAll() {        return $this->redis->keys('*');
    }

    
    public function del($key) {        return $this->redis->delete($key);
    }

    
    public function dels($keyArray) {        if (is_array($keyArray)) {            return $this->redis->del($keyArray);
        } else {            return "Call  " . __FUNCTION__ . " method  parameter  Error !";
        }
    }
    
    
    public function increment($key) {        return $this->redis->incr($key);
    }
    
    
    public function decrement($key) {        return $this->redis->decr($key);
    }
   
    
    
    public function isExists($key){        return $this->redis->exists($key);
    }

    
    public function updateName($key,$newKey){        return $this->redis->RENAMENX($key,$newKey);
    }
    
   
    public function dataType($key){        return $this->redis->type($key);
    }

   
    
    public function flushAll() {        return $this->redis->flushAll();
    }


     
    
    public function redisOtherMethods() {        return $this->redis;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/229766.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号