1.Mysql的varchar(60),60是怎么算的?
一般对varchar的类型就比较模糊,模糊的点在于varchar(60),是60个字符还是60个汉字?
如果是utf8编码,一个汉字占用3个字符,gbk占用两个字符,Sql语句:
mysql> select im_contents from `swoole_msg`.`im_user_chat_record_7` where `chat_id` = '72';
+-------------+
| im_contents |
+-------------+
| 恶趣味 |
+-------------+
1 行于数据集 (0.03 秒)
mysql> select length(im_contents) from `swoole_msg`.`im_user_chat_record_7` where `chat_id` = '72';
+---------------------+
| length(im_contents) |
+---------------------+
| 9 |
+---------------------+
1 行于数据集 (0.09 秒)
公式如下:
//utf编码:
varchar(60) / 3 = 20 (个汉字)
//gbk编码:
varchar(60) / 2 = 30 (个汉字)
2.redis缓存命中率的计算
登陆redis客户端,执行info stats
命令
其中
keyspace_hits:命中的次数
keyspace_misses:没有命中的次数
那么,计算的公式为
keyspace_hits / (keyspace_hits + keyspace_misses)
3.缓存的削峰和过期时间回收
缓存一般多用在并发大的情况下使用,比如下面的例子在获取用户的详细信息中,redis的缓存启到了流量削峰的作用,在设置缓存过期时间的时候也要注意一些事情,如果缓存在同一时间过期,那个流量会直接冲击到Mysql数据库上,也会造成宕机的情况,所以在过期的时候要在一个时间段中随机过期,使流量平滑过渡。
$uid = \EasySwoole\RedisPool\RedisPool::invoke(function (\EasySwoole\Redis\Redis $redis) use ( $token ) {
$uid = $redis->get($token);
if(!isset($uid) || empty($uid)){
//远程验证token
$uid = OAuth::getUserInfo( $token );
if(isset($uid) && !empty($uid) && intval($uid) > 0){
//存入缓存时间,过期时间小于 7300s
$expireTime = 3650+rand(1,3000);
$redis->setEx($token,$expireTime, $uid);
}
return $uid;
}else{
return $uid;
}
},self::REDIS_CONN_NAME );
4.跨域的Ajax允许携带Cookie
在很多跨域请求中有的接口要求携带Cookie,在Ajax中xhrFields这个字段就代表允许传递cookie的值。
function testJsonp(){
$.ajax({
url:"http://192.168.1.88/testAjaxJsonp",
type:"GET",//必须是get请求
dataType:"jsonp",//请求的数据类型
jsonp:"callback",//请求类型是回调
jsonpCallback:"callbackFunction",//数据请求成功时回调的方法
data:{
},//请求的数据
xhrFields: {
withCredentials: true //解决跨服务传递时不传递cookie的问题,允许携带证书
},
success:function(data){//执行完成返回的数据
alert(data.id);//输出值是1
}
});
}
Nginx配置跨域:
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
php 设置跨域:
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段