0%

线上代码出错了,原因是用错了 isset() ,来判断数组的key是否存在,

$arr['real_boss'] = NULL;
$res = isset($arr['real_boss']);
var_dump($res);

结果是 bool(false)

那怎么判断数组key是否存在呢?

key_exists($arr, 'real_boss');
array_key_exists($arr, 'real_boss');
这两个方法功能是一样的,只是PHP版本的区别

缓存,什么是缓存呢?大家想必都非常清楚,缓存就是数据交换的缓冲区,
就程序而言,

Yii 缓存

11.1

Yii::$app 应用主体 Yii::$app->cache 缓存组件 Class yii\caching\FileCache较大块的数据 cachePath 默认runtime/cache 把文件删除,又要5s 运行后又生成 根据存储介质的不同,可分为
  1. FileCache 文件
  2. ApcCache PHP APC 扩展
  3. DbCache 表
  4. DummyCache 仅作为一个缓存占位符
  5. MemCache 使用PHP memcache 和 memcached 扩展
  6. yii\redis\Cache 基于Redis键值对存储
  7. WinCache 使用PHP WinCache 扩展
  8. XCache 使用PHP XCache 扩展
  9. ZendDataCache ZendDataCache扩展

存储介质不同

方法
get,set
缓存API
get 返回false
set key,value

  • add() ,只会在键名不存在的时候才会放到缓存中
  • multiGet()
  • multiSet()
  • multiAdd()
  • exists()
  • delete()
  • flush() 删除所有的缓存数据

组件的可替代性,直接换配置文件的组件
因为使用了同样的API

缓存键
缓存键前缀

keyPrefix

11.2

优化了性能
增加一篇文章,文章条数应该加1,
缓存空间已经满的极端情况,
不合理的,有什么方法干预缓存呢?
缓存过期
优化效果和实时性的矛盾
Yii::$app->cache->set('postCount', $data, 60);

缓存依赖与缓存过期可以一起使用
文章数据发生了变化,刷新缓存
对象,条件
set时,会记下sql的执行结果

  • DbDependency如果指定SQL语句的查询结果发生了变化,则依赖改变。
  • FileDependency如果文件的最后修改时间发生变化,则依赖改变
  • ChainedDependency如果依赖链上任何一个依赖发生变化,则依赖改变
  • ExpressionDependency如果指定的PHP表达式执行结果发生变化,则依赖改变
  • TagDependency将一项缓存数据标记到一个组名,你可以通过调用对象的 invalidate() 一次性将相同组名的缓存全部置为失效状态
难道是更新成 yii\caching\TagDependency 了?
$dependency = new \yii\caching\DbDependency(
    [\'sql\'=>\'select count(id) from post\']
);
Yii::$app->cache->set(\'postCount\', $data, 600, $dependency);
选择合适的依赖条件, 解决实时性,准确反映缓存数据的变化, 计算依赖结果所消耗的资源远小于计算数据结果所消耗的资源 缓存过期,缓存依赖

11.3

片段缓存,页面缓存 标签云的片段
$dependency = new \yii\caching\DbDependency(
    [\'sql\'=>\'select count(id) from post\']
);
if( $this->beginCache(\'cache\', [
    \'duration\' => 600,
    \'dependency\' => $dependency,
]) )
{
    echo TagsCloudWidget::widget([\'tags\'=>$tags]);
    $this->endCache();
}
缓存依赖

缓存嵌套
片段缓存可以被嵌套使用,一个片段缓存可以被另一个嵌套
动态内容,一大段静态内容中有少许动态内容的情况
调用
\yii\base\View::renderDynamic(return 'Yii::$app->user->identity->name;');去插入动态内容

页面缓存
在 behaviors() 方法

\'pageCache\' => [
    \'class\' => \'yii\filters\PageCache\',
    \'only\' => [\'index\'],
    \'duration\' => 600,
    \'dependency\' => [
        \'class\' => \'yii\caching\DbDependency\',
        \'sql\' => \'select count(id) from post\',
    ],
];

点第二页的时候,还是第一页的内容

\'variations\' => [
    Yii::$app->request->get(\'page\'),
];

查询的时候

\'variations\' => [
    Yii::$app->request->get(\'page\'),
    Yii::$app->request->get(\'PostSearch\'),
];

缓存整个页面
也支持缓存过期和缓存依赖
可以在使用页面缓存的同时,使用片段缓存和动态内容
缓存变化
缓存的内容需要根据一些参数的变化而变化
如,Web应用支持多语言
['variations'=>[Yii::$app->language]];

11.4 HTTP 缓存

耗时生成的数据,页面内容缓存在服务器上 利用客户端缓存
behaviors()
\'httpCache\' => [
    \'class\' => \'yii\filters\HttpCache\',
    \'only\' => [\'detail\'],
    \'lastModified\' => function($action, $params)
    {
        $q = new \yii\db\Query();
        return $q->from(\'post\')->max(\'update_time\');
    },
    \'etagSeed\' => function($action, $params)
    {
        $post = $this->findModel(Yii::$app->request->get(\'id\'));
        return serialize([$post->title, $post->conent]);
    },
    \'CacheControlHeader\' => \'public,max-age=600\',
];

Load 3.07s
HTTP 头

  • \yii\filters\HttpCache::lastModified
  • \yii\filters\HttpCache::ctagSeed
  • \yii\filters\HttpCache::cacheControlHeader

HTTP 协议
头和主体
响应头 Last-Modified 头
使用时间戳标明页面自上次客户端缓存后是否被修改过
通过配置lastModified属性向客户端发送Last-Modified头。

ETag头
Hash 值
Cache-Control 头指定了页面的常规缓存策略。
可以通过配置 \yii\filters\HttpCache::cacheControlHeader 属性发送相应的头信息

缓存过期用得最多

  1. 问题,运行PHP时告警
    PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/libevent.so' - /usr/lib64/php/modules/libevent.so: undefined symbol: php_sockets_le_socket in Unknown on line 0
    PHP Warning:  Module 'redis' already loaded in Unknown on line 0
    
  2. 第一个跟libevent.so有关,网上查了一下,把/etc/php.d目录下的libevent.ini重命名为libevent.so即可
    # cd /etc/php.d
    # mv libevent.ini libevent.so
    
    其他报类似错误也可以这样试试 参考http://blog.csdn.net/luckymama/article/details/74668372
  3. 第二个redis重复加载了,查看 php.ini 文件,发现
    extension_dir = "/usr/lib64/php/modules"
    extension = "redis.so"
    
    原来 /usr/lib64/php/modules 目录下也有redis.so,PHP运行时统一加载该目录下的 .so 文件 所以把extension = "redis.so"注释掉,改为
    extension_dir = "/usr/lib64/php/modules"
    ; extension = "redis.so"
    

    参考http://www.jb51.net/article/62281.htm

  1. 安装crontab

    yum -y install vixie-cron

  2. 相关命令
    # crontab –l //查看crontab项
    # crontab –e //编辑
    
  3. 服务操作
    //重启服务
    # /etc/init.d/crond restart 
    //查看服务状态
    # /etc/init.d/crond status
    
    这和很多服务类似
  4. 日志
    # cat /var/log/cron
    
  5. 实例
    #每分钟一次
    * * * * * sudo /usr/share/nginx/html/blog/yii crontab/send
    #这个是每小时的01分执行,之前以为是每分钟,一直以为有问题
    1 * * * * sudo /usr/share/nginx/html/blog/yii crontab/send
    
  6. 添加crontab命令后,重启crontab
  7. 参考http://www.cnblogs.com/intval/p/5763929.html
除了 crontab -l 
定时任务另一个位置
/etc/crontab

  1. 直接把配置文件main-local.php放到git exclude里,但没有把main.php没有的配置拷贝过去, 导致数据库链接失败
  2. <li>
    直接删了两个引用local文件的语句,没有检查语法错误。原来的:
    <pre>
    $config = yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/../../common/config/main.php'),
        require(__DIR__ . '/../../common/config/main-local.php'),
        require(__DIR__ . '/../config/main.php'),
        require(__DIR__ . '/../config/main-local.php')
    );
    </pre>
    删了之后:
    <pre>
    $config = yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/../../common/config/main.php'),
        require(__DIR__ . '/../config/main.php'),
    );
    </pre>
    漏了一个逗号没删,有语法错误,和平时数组的一项没删是有区别的。
    
            $arr = [
                "a" => 1,
                "b" => 2, //末尾有个,号是可以的
            ];
    
  3. 出错时排错的方法,到nginx的日志查看错误: /var/log/nginx nginx错误日志目录可以从nginx的配置文件中看, 配置目录:/etc/nginx

常用的Linux命令,备查,欢迎在评论区与我分享!
# 上传文件
rz -bey
# 清理磁盘
du -h --max-depth=1
# 磁盘是否满了
df -hl
# 去掉环境变量
unset 
env 查看
# grep 含引号
grep 'retCode":"10002"' rlog.log
# 查看谁登录了
w
#查看指定行
cat crm/protected/modules/Tumall/controllers/SellerController.php | tail -n +494 | head -n 10
#测试网络
echo "" | telnet 192.168.3.97 11218
# 搜索排除注释
grep -E "^$|^#" -v grep.txt
#查看文件某些行
sed -n '349,360p' basic/components/webservice.php
sed -n 'a,bp' basic/components/webservice.php, 查看 a-b行,如果b 覆盖, >> 追加
#grep显示附件的内容,-A,-B,-C
cat include/connectDB.inc.php |grep -C 5 fileServer
grep -A 5 zoneTypes src/To8to/CommonBundle/config/CategoryConfig.inc.php
#查找目录下哪些文件包含某个字符串
 grep -r m.to8to.com conf/conf.d/
#curl命令POST请求
curl --data "param1=value1¶m2=value2" https://example.com/resource.cgi
#查看Linux系统发行版本
cat /etc/issue
#查看Debian版本
more /etc/debian_version
#查找文件
find / -name curl_for_test.txt
find /usr/ -name php7
#修改~vimrc后怎样立刻生效
:source ~/.vimrc (你得确定你的vimrc的路径)
#cpu
root@nginx-php-fastcgi ~# cat /proc/cpuinfo| grep "cpu cores"| uniq
cpu cores	: 1
root@nginx-php-fastcgi ~# cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
1
root@nginx-php-fastcgi ~# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
      1  Intel(R) Core(TM) i3-4170 CPU @ 3.70GHz
root@nginx-php-fastcgi ~# cat /proc/cpuinfo| grep "processor"| wc -l
1
root@nginx-php-fastcgi ~# 
#查找
grep access /etc/nginx/conf.d/shejiben.conf
#awk
#备份文件并验证
root@nginx-php-fastcgi ~/backup# cp my.conf my.conf`date +%F` 
root@nginx-php-fastcgi ~/backup# md5sum my.conf my.conf2018-05-28 
8cd2616e051a7b8aea043c437e9c2b49  my.conf
8cd2616e051a7b8aea043c437e9c2b49  my.conf2018-05-28
#建目录
root@nginx-php-fastcgi /tmp# mkdir -p /tmp/mini_program/{aa,bb}
#查看文件大小等信息
stat img/xcx/interface_img.jpg 
#查看用户
cat /etc/passwd | grep www-data
#远程文件传输
scp root@192.168.1.233:/root/mobileapi2.lua ./
#文件夹
scp -r root@192.168.1.233:/root/mobileapi2.lua ./
指定端口
scp -P 10086 root@192.168.1.154:/usr/local/java/abc.zip ./
#登录
ssh root@192.168.0.11
#合并两个文件夹的文件
rsync -av /source/ /destination/
#合并后删除源文件夹
rm -rf /source/
#开启自启动
vi /etc/rc.d/rc.local
加入自启动的服务,如: service nginx start
另一个命令:chkconfig 
#包相关的目录文件
dpkg -L
#查看开机启动程序
vim /etc/rc.local
#切换到root
sudo -i
#创建/usr/local/php/bin/php的软链接为php53
ln -s /usr/local/php/bin/php /usr/bin/php53
#cut截取
 git branch -a | grep "remotes/origin" | cut -nb 18-50 
#查看Java进程所在目录
[root@java154 ~]# pidof java | xargs pwdx
#crontab
直接改文件可以 vim /etc/crontab 
但crontab -e 就不生效

[root@localhost ~]# cat ~/.bash_history

[root@localhost ~]# ls -ld /var/www/html/

[root@localhost ~]# head -10 phpinfo.txt

[root@localhost ~]# cat test.txt | tr [a-z] [A-Z]

[root@localhost ~]# wc -l /etc/passwd

[root@localhost ~]# cut -d: -f1 /etc/passwd

[root@localhost ~]# diff --brief log.txt index.html

[root@localhost ~]# ls -l test.txt 
-rw-r--r--. 1 root root 10 Feb  1 03:57 test.txt
[root@localhost ~]# echo "I'm hacker" >> test.txt 
[root@localhost ~]# ls -l test.txt 
-rw-r--r--. 1 root root 21 Feb  1 04:07 test.txt
[root@localhost ~]# touch -d "2019-02-01 03:57" test.txt 
[root@localhost ~]# ls -l test.txt 
-rw-r--r--. 1 root root 21 Feb  1 03:57 test.txt

[root@localhost ~]# dd if=/dev/zero of=10M_file count=1 bs=10M

[root@localhost ~]# file /dev/zero

[root@localhost ~]# grep -v /sbin/nologin /etc/passwd #反选

#将标准输出和错误输出共同写入到文件中
0  17  *  *  * root php /var/www/html/advanced/yii task/log >/dev/null 2>&1


[root@localhost ~]# ls -l /etc/ | more
#查询绝对路径
[root@xhs ~]# whereis php
#查进程的线程数
ps -Lf 21371 | wc -l
#查不包含某字符串的文件
ls | grep Deal -v | wc -l
#历史命令不显示行号
history | cut -c 8-
history | tail | cut -c 8-
#创建公钥
ssh-keygen
#查某个java服务
ps -ef | grep `jps | grep gdm | awk '{print $1}'`

  1. 首先到官网Yii Advanced 模板的git地址下载web服务器需要的配置文件
  2. 修改域名和代码目录,以nginx为例,我的域名是xiaohuasheng.cc,本地代码放在,C:\Users\xhs\blog, 所以 nginx 配置修改后如下:
            server {
            charset utf-8;
            client_max_body_size 128M;
    
    
        listen 80; ## listen for ipv4
        #listen [::]:80 default_server ipv6only=on; ## listen for ipv6
    
        server_name xiaohuasheng.cc;
        root   C:/Users/xhs/blog/frontend/web/;
        index       index.php;
    
        #access_log  /path/to/yii-application/log/frontend-access.log;
        #error_log   /path/to/yii-application/log/frontend-error.log;
    
        location / {
            # Redirect everything that isn't a real file to index.php
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        # uncomment to avoid processing of calls to non-existing static files by Yii
        #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        #    try_files $uri =404;
        #}
        #error_page 404 /404.html;
    
        # deny accessing php files for the /assets directory
        location ~ ^/assets/.*\.php$ {
            deny all;
        }
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/var/run/php5-fpm.sock;
            try_files $uri =404;
        }
    
        location ~* /\. {
            deny all;
        }
    }
     
    server {
        charset utf-8;
        client_max_body_size 128M;
    
        listen 80; ## listen for ipv4
        #listen [::]:80 default_server ipv6only=on; ## listen for ipv6
    
        server_name admin.xiaohuasheng.cc;
        root   C:/Users/xhs/blog/backend/web/;
        index       index.php;
    
        #access_log  /path/to/yii-application/log/backend-access.log;
        #error_log   /path/to/yii-application/log/backend-error.log;
    
        location / {
            # Redirect everything that isn't a real file to index.php
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        # uncomment to avoid processing of calls to non-existing static files by Yii
        #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        #    try_files $uri =404;
        #}
        #error_page 404 /404.html;
    
        # deny accessing php files for the /assets directory
        location ~ ^/assets/.*\.php$ {
            deny all;
        }
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/var/run/php5-fpm.sock;
            try_files $uri =404;
        }
    
        location ~* /\. {
            deny all;
        }
    }
    
    </pre>
    </li>
    <li>本地没问题了上传配置文件<code>xiaohuasheng.conf</code>到服务器nginx配置文件目录<code>/etc/nginx/conf.d</code></li>
    <li>对应修改一下配置文件的root目录,CentOS服务器上的配置如下:
    <pre>
        server {
        charset utf-8;
        client_max_body_size 128M;
    
        listen 80; ## listen for ipv4
        #listen [::]:80 default_server ipv6only=on; ## listen for ipv6
    
        server_name xiaohuasheng.cc;
        root   /usr/share/nginx/html/blog/frontend/web/;
        index       index.php;
    
        #access_log  /path/to/yii-application/log/frontend-access.log;
        #error_log   /path/to/yii-application/log/frontend-error.log;
    
        location / {
            # Redirect everything that isn't a real file to index.php
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        # uncomment to avoid processing of calls to non-existing static files by Yii
        #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        #    try_files $uri =404;
        #}
        #error_page 404 /404.html;
    
        # deny accessing php files for the /assets directory
        location ~ ^/assets/.*\.php$ {
            deny all;
        }
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/var/run/php5-fpm.sock;
            try_files $uri =404;
        }
    
        location ~* /\. {
            deny all;
        }
    }
     
    server {
        charset utf-8;
        client_max_body_size 128M;
    
        listen 80; ## listen for ipv4
        #listen [::]:80 default_server ipv6only=on; ## listen for ipv6
    
        server_name admin.xiaohuasheng.cc;
        root   /usr/share/nginx/html/blog/backend/web/;
        index       index.php;
    
        #access_log  /path/to/yii-application/log/backend-access.log;
        #error_log   /path/to/yii-application/log/backend-error.log;
    
        location / {
            # Redirect everything that isn't a real file to index.php
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        # uncomment to avoid processing of calls to non-existing static files by Yii
        #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        #    try_files $uri =404;
        #}
        #error_page 404 /404.html;
    
        # deny accessing php files for the /assets directory
        location ~ ^/assets/.*\.php$ {
            deny all;
        }
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/var/run/php5-fpm.sock;
            try_files $uri =404;
        }
    
        location ~* /\. {
            deny all;
        }
    }
    
    </pre>
    </li>
    <li>重启nginx <code>/etc/init.d/nginx restart</code>,或者<code>service nginx restart</code>
    </li>
    <li>把yii URL manager 的代码放进 common/config/main.php,供参考:
    

    'urlManager' => [
       
    'enablePrettyUrl' => true,
       
    'showScriptName' => false,
      
    'suffix'=>'.html',
       
    'rules' => [
       
    '<controller:\w+>/<id:\d+>'=>'<controller>/detail',
       
    'posts'=>'post/index',
       
    ],
    ],

  3. 我的域名是租腾讯云的,为了能访问二级域名admin.xiaohuasheng.cc,在腾讯云的域名解析添加规则
            
    
    记录类型 主机记录	线路类型	记录值	MX优先级	TTL(秒)	最后操作时间	操作
    A	admin	默认	114.215.111.84	-	600	2017-10-07 16:52:17	修改暂停删除
    </pre>