B站封面提取 http://b.qiuyeye.cn/

http://b.qiuyeye.cn/

bilibili封面提取  哔哩哔哩封面提取 bilibili封面提取-b站封面提取

PHP冒泡排序和快速排序

function maopao($arr)
{
    if (!is_array($arr)) {
        return false;
    }
    for ($j = 0; $j < count($arr); $j++) {
        for ($i = 0; $i < count($arr); $i++) {
            if ($arr[$j] > $arr[$i]) {
                $tem = $arr[$j];
                $tem2 = $arr[$i];
                $arr[$i] = $tem;
                $arr[$j] = $tem2;
            }
        }
    }
    return $arr;
}


function quicksort($arr)
{
    if (!is_array($arr)) {
        return false;
    }
    if (count($arr) == 0) {
        return $arr;
    }
    $tag = $arr[0];
    $left_arr = $right_arr = array();
    for ($i = 1; $i < count($arr); $i++) {
        if ($tag > $arr[$i]) {
            $left_arr[] = $arr[$i];
        } else if ($tag == $arr[$i]) {
            $left_arr[] = $arr[$i];
        } else if ($tag < $arr[$i]) {
            $right_arr[] = $arr[$i];
        }
    }
    $left_arr = quicksort($left_arr);
    $right_arr = quicksort($right_arr);
    return (array_merge($left_arr, array($tag), $right_arr));
}

PHP Fatal error: Class ‘DOMDocument’ not found 解决方法

Centos方法
运行
yum install php-xml
// 如果是php5.6等版本的话,可以尝试下面的安装
yum install php56w-xml

安装完重启nginx php
service nginx restart
service php-fpm restart

算法复杂度

php redis秒杀功能

<?php
header("content-type:text/html;charset=utf-8");
$redis = new redis();
$result = $redis->connect('127.0.0.1', 7379);
$redis->watch("mywatchlist");
$len = $redis->hlen("mywatchlist");
$rob_total = 100; //抢购数量
if ($len < $rob_total) {
    $redis->multi();
    $redis->hSet("mywatchlist", "user_id_" . mt_rand(1, 999999), time());
    $rob_result = $redis->exec();
    file_put_contents("log.txt", $len . PHP_EOL, FILE_APPEND);
    if ($rob_result) {
        $mywatchlist = $redis->hGetAll("mywatchlist");
        echo "抢购成功!<br/>";
        echo "剩余数量:" . ($rob_total - $len - 1) . "<br/>";
        echo "用户列表:<pre>";
        var_dump($mywatchlist);
    } else {
        echo "手气不好,再抢购!";
        exit;
    }
} else {
    echo "已卖光!";
    exit;
}
?>

 

CentOS下安装Redis及Redis的PHP扩展

1、安装Redis

1.1 如果没有安装wget,安装wget

yum install wget

1.2 在http://redis.io/download页面查看redis版本,并下载安装

wget http://download.redis.io/releases/redis-3.2.0.tar.gz

1.3 解压,并进入解压目录进行编译。编译成功后会在redis-3.2.0目录下生成相关文件

$ tar xzf redis-3.2.0.tar.gz
$ cd redis-3.2.0
$ make

如果make时没有发现gcc,那么安装gcc

yum install gcc gcc-c++ kernel-devel

再次make,如果出现如下错误

zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"

则使用如下命令进行make

make MALLOC=libc

1.4 在文件夹redis-3.2.0下启动redis服务,输入如下命令后回车。

./src/redis-server redis.conf &

1.4 检测

复制代码
#检测后台进程是否存在
ps -ef |grep redis

#检测6379端口是否在监听
netstat -lntp | grep 6379

#使用`redis-cli`客户端检测连接是否正常
./src/redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set key "hello world"
OK
127.0.0.1:6379> get key
"hello world"
复制代码

1.5 停止服务

#使用客户端
./src/redis-cli shutdown
#因为Redis可以妥善处理SIGTERM信号,所以直接kill -9也是可以的
kill -9 PID

 

2、安装Redis的PHP扩展

2.1 安装phpize

yum install php-devel

2.2 下载扩展源码包,直接用wget

#wget下载github上的文件  
wget https://github.com/nicolasff/phpredis/archive/master.zip

2.3 如果没装unzip,需要先安装unzip

yum install unzip

2.4 解压master.zip

unzip master.zip

2.5 解压目录为phpredis-master,进入该文件夹,开始编译php扩展

phpize

2.6 配置环境

./configure

2.7 编译

make && make install

编译完成后显示:

Build complete.  
Don't forget to run 'make test'.  
Installing shared extensions:     /usr/lib64/php/modules/

进入/usr/lib64/php/modules/文件夹,发现redis.so的扩展

2.8 修改/etc/php.ini,添加下面的扩展

extension=redis.so

2.9 重启服务器

service httpd restart

最后查看phpinfo,显示如下,代表安装成功:

3、PHP代码测试

复制代码
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('name','zhou', 10);
$key_1 = $redis->get('name');
echo $key_1;
?>

redis 五种数据结构详解(string,list,set,zset,hash)

Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。

String——字符串
Hash——字典
List——列表
Set——集合
Sorted Set——有序集合

下面我们就来简单说明一下它们各自的使用场景:

一、Redis String类型

string类型的数据存储是最简单的key-value存储;

1.string字符串读写实现方式:

    public function testRedis(){
        //string类型的数据结构
        app()->redis->set('1', 'aa');
    //根据key取出value值
    $string = app()->redis->get('1');
    }

2.redis客户端查看结果:

3.string字符串的其他redis操作方法:

//普通set/get操作
$redis->set(‘library’, ‘predis’);
$retval = $redis->get(‘library’);
echo $retval; //显示 ‘predis’

//setex set一个存储时效
$redis->setex(‘str’, 10, ‘bar’); //表示存储有效期为10秒

//setnx/msetnx相当于add操作,不会覆盖已有值
$redis->setnx(‘foo’,12); //true
$redis->setnx(‘foo’,34); //false

//getset操作,set的变种,结果返回替换前的值
$redis->getset(‘foo’,56);//返回34

// incrby/incr/decrby/decr 对值的递增和递减
$redis->incr(‘foo’); //foo为57
$redis->incrby(‘foo’,2); //foo为59

//exists检测是否存在某值
$redis->exists(‘foo’);//true

//del 删除
$redis->del(‘foo’);//true

//type 类型检测,字符串返回string,列表返回 list,set表返回set/zset,hash表返回hash
$redis->type(‘foo’);//不存在,返回none
$redis->set(‘str’,’test’);
$redis->type(‘str’); //字符串,返回string

继续阅读“redis 五种数据结构详解(string,list,set,zset,hash)”

MySQL索引原理及慢查询优化

MySQL凭借着出色的性能、低廉的成本、丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库。虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会从职位描述上看到诸如“精通MySQL”、“SQL语句优化”、“了解数据库原理”等要求。我们知道一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,所以查询语句的优化显然是重中之重。
本人从13年7月份起,一直在美团核心业务系统部做慢查询的优化工作,共计十余个系统,累计解决和积累了上百个慢查询案例。随着业务的复杂性提升,遇到的问题千奇百怪,五花八门,匪夷所思。本文旨在以开发工程师的角度来解释数据库索引的原理和如何优化慢查询。

一个慢查询引发的思考

select
   count(*) 
from
   task 
where
   status=2 
   and operator_id=20839 
   and operate_time>1371169729 
   and operate_time<1371174603 
   and type=2;

系统使用者反应有一个功能越来越慢,于是工程师找到了上面的SQL。 继续阅读“MySQL索引原理及慢查询优化”

【转载】别被忽悠,php是有毫秒时间戳的!

今天要用在php里面记录一个毫秒时间戳。
从网上找了一下,很多人都说php没有毫秒时间戳

大体都用这样的方法取时间戳:

/** 获取当前时间戳,精确到毫秒 */
function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

php真的这么弱???我不信!!!
研究了一番,其实就在microtime里面!!!

/**
 * Return current Unix timestamp with microseconds
 * @link http://php.net/manual/en/function.microtime.php
 * @param bool $get_as_float [optional] <p>
 * When called without the optional argument, this function returns the string
 * "msec sec" where sec is the current time measured in the number of
 * seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and
 * msec is the microseconds part.
 * Both portions of the string are returned in units of seconds.
 * </p>
 * <p>
 * If the optional get_as_float is set to
 * true then a float (in seconds) is returned.
 * </p>
 * @return mixed 
 * @since 4.0
 * @since 5.0
 */
function microtime ($get_as_float = null) {}

关键 传递参数get_as_float = true:

If the optional get_as_float is set to true then a float (in seconds) is returned.
如果参数get_as_float被设置成true,将返回float(单位秒)。

When called without the optional argument, 
this function returns the string "msec sec" 
where sec is the current time measured in the number of seconds 
since the Unix Epoch (0:00:00 January 1, 1970 GMT),
and msec is the microseconds part.
当不带参数调用的时候,函数返回一个“毫秒 秒”的字符串,
其中秒是从1979年1月1日0时到现在的秒数,另一部分是毫秒。

动手写个测试很快的:

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$t1 = microtime_float();
$t2 = microtime(false);
$t3 = microtime(true);
$tInt = (int)(microtime(true)*1000);
echo "t1 = $t1<p> t2 = $t2 <p> t3 = $t3 <p>";
echo "tInt = $tInt <p>";

输出:

t1 = 1486474356.4957 
t2 = 0.49571200 1486474356 
t3 = 1486474356.4957
tInt = 1486474356495

看来只会用百度的程序员不是好领导。
不要停止质疑和探寻。


作者:曹建峰
链接:http://www.jianshu.com/p/ad4a2b502d88
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。