#list list-max-ziplist-entries 512 #表示允许包含的最大元素数量 list-max-ziplist-value 64 #表示压缩节点允许存储的最大体积 #hash #当超过任一限制后,将不会使用ziplist方式进行存储 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 #zset zset-max-ziplist-entries 128 zset-max-ziplist-value 64
#test.php
<?php
$redis=new Redis();
$redis->connect('192.168.95.11','6379');
for ($i=0; $i<512 ; $i++)
{
$redis->lpush('test-list',$i.'-test-list'); #往test-list推入512条数据
}
?>
#test.php
<?php
$redis=new Redis();
$redis->connect('192.168.95.11','6379');
for ($i=0; $i<512 ; $i++)
{
$redis->sadd('test-set',$i); #给集合test-set插入512个member
}
?>
#test1.php
<?php
header("content-type: text/html;charset=utf8;");
$redis=new Redis();
$redis->connect('192.168.95.11','6379');
$start=time();
for ($i=0; $i<50000 ; $i++)
{
$redis->lpush('test-list',$i.'-aaaassssssddddddkkk');
}
$end=time();
echo "插入耗时为:".($end-$start).'s';
?>
#test2.php
<?php
header("content-type: text/html;charset=utf8;");
$redis=new Redis();
$redis->connect('192.168.95.11','6379');
$start=time();
$num=0;
while($redis->rpoplpush('test-list','test-new'))
{
$num+=1;
}
echo '执行次数为:'.$num."<br/>";
$end=time();
echo "耗时为:".($end-$start).'s';
?>
<?php
class ShardHash
{
private $redis=''; #存储redis对象
/**
* @desc 构造函数
*
* @param $host string | redis主机
* @param $port int | 端口
*/
public function __construct($host,$port=6379)
{
$this->redis=new Redis();
$this->redis->connect($host,$port);
}
/**
* @desc 计算某key的分片ID
*
* @param $base string | 基础散列
* @param $key string | 要存储到分片散列里的键名
* @param $total int | 预计非数字分片总数
*
* @return string | 返回分片键key
*/
public function shardKey ($base,$key,$total)
{
if(is_numeric($key))
{
$shard_id=decbin(substr(bindec($key),0,5)); #取$key二进制高五位的十进制值
}
else
{
$shard_id=crc32($key)%$shards; #求余取模
}
return $base.'_'.$shard_id;
}
/**
* @desc 分片式散列hset操作
*
* @param $base string | 基础散列
* @param $key string | 要存储到分片散列里的键名
* @param $total int | 预计元素总数
* @param $value string/int | 值
*
* @return bool | 是否hset成功
*/
public function shardHset($base,$key,$total,$value)
{
$shardKey=$this->shardKey($base,$key,$total);
return $this->redis->hset($shardKey,$key,$value);
}
/**
* @desc 分片式散列hget操作
*
* @param $base string | 基础散列
* @param $key string | 要存储到分片散列里的键名
* @param $total int | 预计元素总数
*
* @return string/false | 成功返回value
*/
public function shardHget($base,$key,$total)
{
$shardKey=$this->shardKey($base,$key,$total);
return $this->redis->hget($shardKey,$key);
}
}
$obj=new ShardHash('192.168.95.11');
echo $obj->shardHget('hash-','key',500);
?>
<?php
class ShardSet
{
private $redis=''; #存储redis对象
/**
* @desc 构造函数
*
* @param $host string | redis主机
* @param $port int | 端口
*/
public function __construct($host,$port=6379)
{
$this->redis=new Redis();
$this->redis->connect($host,$port);
}
/**
* @desc 根据基础键以及散列包含的键计算出分片键
*
* @param $base string | 基础散列
* @param $key string | 要存储到分片散列里的键名
* @param $total int | 预计分片总数
*
* @return string | 返回分片键key
*/
public function shardKey ($base,$member,$total=512)
{
$shard_id=crc32($member)%$shards; #求余取模
return $base.'_'.$shard_id;
}
/**
* @desc 计算唯一用户日访问量
*
* @param $member int | 用户唯一标识符
*
* @return string | ok表示count加1 false表示用户今天已经访问过不加1
*/
public function count($member)
{
$shardKey=$this->shardKey('count',$member,$total=10); #$totla调小一点用于测试
$exists=$this->redis->sismember($shardKey,$member);
if(!$exists) #判断member今天是否访问过
{
$this->redis->sadd($shardKey,$member);
$this->redis->incr('count');
$ttl1=$this->redis->ttl('count');
if($ttl1===-1)
$this->redis->expireat('count',strtotime(date('Y-m-d 23:59:59'))); #设置过期时间
$ttl2=$this->redis->ttl($shardKey);
if($ttl2===-1)
{
$this->redis->expireat("$shardKey",strtotime(date('Y-m-d 23:59:59'))); #设置过期时间
#echo $shardKey; #测试使用
}
#echo $shardKey; #测试使用
return 'ok';
}
return 'false';
}
}
$str=substr(md5(uniqid()), 0, 8); #取出前八位
#将$str作为客户的唯一标识符
$str=hexdec($str); #将16进制转换为十进制
$obj=new ShardSet('192.168.95.11');
$obj->count($str);
?>
<?php
#打包存储字节
#存储用户国家、省份信息
class PackBytes
{
private $redis=''; #存储redis对象
/**
* @desc 构造函数
*
* @param $host string | redis主机
* @param $port int | 端口
*/
public function __construct($host,$port=6379)
{
$this->redis=new Redis();
$this->redis->connect($host,$port);
}
/**
* @desc 处理并缓存国家省份数据
* @param $countries string | 第一类数据,国家字符串
* @param $provinces 二维array | 第二类数据,各国省份数组
* @param $cache 1/0 | 是否使用缓存,默认0不使用
*
* @return array | 返回总数据
*/
public function dealData($countries,$provinces,$cache=0)
{
if($cache)
{
$result=$this->redis->get('cache_data');
if($result)
return unserialize($result);
}
$arr=explode(' ',$countries);
$areaArr[]=$arr;
$areaArr[]=$provinces;
$cache_data=serialize($areaArr);
$this->redis->set('cache_data',$cache_data);
return $areaArr;
}
/**
* @desc 将具体信息按表索引转换成编码信息
*
* @param $countries,$provinces,$cache| 参考dealData方法
* @param $country string | 具体信息--国家
* @param $province string | 具体信息--省份
*
* @return string | 返回转换的编码信息
*/
public function getCode($countries,$provinces,$country,$province,$cache=0)
{
$dataArr=$this->dealData($countries,$provinces,$cache=0);
$result=array_search($country, $dataArr[0]); #查找数组中是否含有data1
if($result===false) #判断是否存在
return chr(0).chr(0); #不存在则返回初始值
$code=chr($result);
$result=array_search($province, $dataArr[1][$country]); #查找数组中是否含有data2
if($result===false)
return $code.chr(0);
return $code.chr($result); #返回对应ASCII(0~255)所指定的字符
}
/**
* @desc 计算用户存储编码数据的相关位置信息
*
* @param $userID int | 用户的ID
*
* @return array | 返回一个数组 包含数据存储时的分片ID、以及属于用户的存储位置(偏移量)
*/
public function savePosition($userID)
{
$shardSize=pow(2, 3); #每个分片的大小
$position=$userID*2; #user的排位
$arr['shardID']=floor($position/$shardSize); #分片ID
$arr['offset']=$position%$shardSize; #偏移量
return $arr;
}
/**
* @desc | 整合方法,将编码信息存入redis中string相应的位置
*
* @param $userID int | 用户ID
* @param $countries string | 第一类数据,国家字符串
* @param $provinces 二维array | 第二类数据,各国省份数组
* @param $country string | 具体信息--国家
* @param $province string | 具体信息--省份
* @param $cache 1/0 | 是否使用缓存,默认0不使用
*
* @return 成功返回写入位置/失败false
*/
public function saveCode($userID,$countries,$provinces,$country,$province,$cache=0)
{
$code=$this->getCode($countries,$provinces,$country,$province,$cache=0);
$arr=$this->savePosition($userID); #存储相关位置信息
return $this->redis->setrange('save_code_'.$arr['shardID'],$arr['offset'],$code);
}
/**
* @desc 获取用户的具体国家与省份信息
*
* @param $userID int | 用户ID
*
* @return array | 返回包含国家和省份信息的数组
*/
public function getMessage($userID)
{
$position=$this->savePosition($userID);
$code=$this->redis->getrange('save_code_'.$position['shardID'],$position['offset'],$position['offset']+1);
$arr=str_split($code);
$areaArr=$this->dealData('', '',$cache=1); #使用缓存数据
$message['country']=$areaArr[0][ord($arr[0])];
$message['province']=$areaArr[1][$message['country']][ord($arr[1])];
return $message;
}
}
header("content-type: text/html;charset=utf8;");
$countries="无 中国 日本 越南 朝鲜 俄罗斯 巴基斯坦 美国";
$provinces=array(
'无'=>array('无'),
'中国'=>array('无','广东','湖南','湖北','广西','云南','湖南','河北'),
'日本'=>array('无','龟孙子区','王八区','倭国鬼区','鬼子区','萝卜头区'),
);
$obj=new PackBytes('192.168.95.11');
/*
#数据处理,并将其缓存到redis中
$b=$obj->dealData($countries,$provinces);
echo "<pre>";
print_r($b);
echo "</pre>";die;
*/
/*
#存储用户国家省份信息
$country='中国';
$province='广东';
$result=$obj->saveCode(0,$countries,$provinces,$country,$province);
echo "<pre>";
print_r($result);
echo "</pre>";
*/
/*
#取出用户国家省份信息
$a=$obj->getMessage(15);
echo "<pre>";
print_r($a);
echo "</pre>";die;
*/
?>
| userID | 国家 | 省份 |
| 0 | 中国 | 广东 |
| 13 | 日本 | 龟孙子区 |
| 15 | 日本 | 王八区 |
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有