From 86c1b949e3b5b8a0b148000afd3831f39a0c41ac Mon Sep 17 00:00:00 2001 From: colly <576734462@qq.com> Date: Sat, 29 Jul 2017 16:35:35 +0800 Subject: [PATCH] 增加redis和上传接口 --- Yfs/Domain/Data.php | 35 ++ Library/Redis/README.md | 130 ++++++++++ Yfs/Model/Data.php | 8 Library/Redis/Lite.php | 481 ++++++++++++++++++++++++++++++++++++++++ Yfs/Api/Data.php | 38 +++ Library/Redis/Config/app.php | 27 ++ 6 files changed, 719 insertions(+), 0 deletions(-) diff --git a/Library/Redis/Config/app.php b/Library/Redis/Config/app.php new file mode 100644 index 0000000..9806344 --- /dev/null +++ b/Library/Redis/Config/app.php @@ -0,0 +1,27 @@ +<?php +/** + * Redis������������ + * @author: ������������ <wenzhenxi@vip.qq.com> 2015-11-15 + */ + +return array( + //Redis��������� + 'redis' => array( + //Redis��������������� + 'servers' => array( + 'host' => '127.0.0.1', //Redis��������������� + 'port' => '6379', //Redis��������� + 'prefix' => 'yfs_', //Redis-key������ + 'auth' => 'yfs_moral_password2017', //Redis������������ + ), + // Redis������������������ + 'DB' => array( + 'developers' => 1, + 'user' => 2, + 'code' => 3, + ), + //���������������������������������������������������/��� + 'blocking' => 5, + ), + +); diff --git a/Library/Redis/Lite.php b/Library/Redis/Lite.php new file mode 100644 index 0000000..6978a12 --- /dev/null +++ b/Library/Redis/Lite.php @@ -0,0 +1,481 @@ +<?php + +/** + * Redis ��������� + * @author: ������������ <wenzhenxi@vip.qq.com> 2015-11-15 + * + * @author: Axios <axioscros@aliyun.com> 2016-09-01 + * + * @update 2016-09-01 + * 1. ������__call������������������������������������������������������DB������������������������������������������������������������������������DB + * 2. ������save_time������,������������������������key���value������������������������������,������������: DI()->redis->save_time($key, $value,$dbname); + * 3. ������counter_forever���������������������������������������������1���������������: DI()->redis->counter_forever($key,$dbname); + * 4. ������counter_time_create������������������������������������������������������������������������������������������: DI()->redis->counter_time_create($key,$expire,$dbname); + * 5. ������counter_time_update���������������������������������������������������1���������������: DI()->redis->counter_time_update($key,$dbname); + * 6. ������������bug���get_time_ttl������������$this->unformatValue($value)���������$value������������������������������������������������false + * + */ +class Redis_Lite extends PhalApi_Cache_Redis{ + + private $db_old; + + /** + * ���������������������������DB + * + * @param $name + * @param $arguments + * @return mixed + * + * @author: Axios <axioscros@aliyun.com> 2016-09-01 + */ + public function __call($name, $arguments) + { + $last = count($arguments)-1; + $dbname = $arguments[$last]; + $this->switchDB($dbname); + unset($arguments[$last]); + $arguments = empty($arguments)? array():$arguments; + return call_user_func_array(array($this,$name),$arguments); + } + + //---------------------------------------------------string������------------------------------------------------- + /** + * ���value ���������������key,��������������������� ������������������������������ + */ + protected function set_forever($key, $value){ + return $this->redis->set($this->formatKey($key), $this->formatValue($value)); + } + + /** + * ������value ������������������������������ + */ + protected function get_forever($key){ + $value = $this->redis->get($this->formatKey($key)); + return $value !== FALSE ? $this->unformatValue($value) : NULL; + } + + /** + * ������������������������������������ + */ + protected function set_time($key, $value, $expire = 600){ + return $this->redis->setex($this->formatKey($key), $expire, $this->formatValue($value)); + } + + + /** + * ������������������������key���value������������������������ + * @author Axios <axioscros@aliyun.com> + */ + protected function save_time($key, $value) + { + if($this->get_exists($key)){ + $ttl = $this->get_time_ttl($key); + return $this->set_time($key,$value,$ttl); + } + + return NULL; + } + + /** + * ������get/set������,������set_Time������get_Time + */ + protected function get_time($key){ + $value = $this->redis->get($this->formatKey($key)); + return $value !== FALSE ? $this->unformatValue($value) : NULL; + } + + /** + * ������������key��������������� + */ + protected function get_time_ttl($key){ + $value = $this->redis->ttl($this->formatKey($key)); + return $value !== FALSE ? $value : NULL; + } + + /** + * ������������k-v,���������v��������������������� ������������ + * array('key0' => 'value0', 'key1' => 'value1') + */ + protected function set_list($value){ + $data = array(); + foreach($value as $k => $v){ + $data[$this->formatKey($k)] = $this->formatValue($v); + } + return $this->redis->mset($data); + } + + /** + * ������������k-v,���������k��������������������� + */ + protected function get_list($key){ + $data = array(); + foreach($key as $k => $v){ + $data[] = $this->formatKey($v); + } + $rs = $this->redis->mget($data); + foreach($rs as $k => $v){ + $rs[$k] = $this->unformatValue($v); + } + return $rs; + } + + /** + * ������key��������������������� true ������ false + */ + protected function get_exists($key){ + return $this->redis->exists($this->formatKey($key)); + } + + /** + * ������������key������������������value������key + */ + protected function get_getSet($key, $value){ + $value = $this->redis->getSet($this->formatKey($key), $this->formatValue($value)); + return $value !== FALSE ? $this->unformatValue($value) : NULL; + } + + /** + * string������������key���string���������������������value + */ + protected function set_append($key, $value){ + return $this->redis->append($this->formatKey($key), $this->formatValue($value)); + } + + /** + * ������������key������������������value������key + */ + protected function get_strlen($key){ + return $this->redis->strlen($this->formatKey($key)); + } + + /** + * ������������ + * value������������������������1 + */ + protected function get_incr($key, $value = 1){ + return $this->redis->incr($this->formatKey($key), $value); + } + + /** + * ������������ + * value������������������������1 + */ + protected function get_decr($key, $value = 1){ + return $this->redis->decr($this->formatKey($key), $value); + } + //------------------------------------------------List������------------------------------------------------- + + /** + * ������������������ ������������������������������ + */ + protected function set_lPush($key, $value){ + return $this->redis->lPush($this->formatKey($key), $this->formatValue($value)); + } + + /** + * ������������������ ������value��������������������������� ������������������������������ + */ + protected function set_lPushx($key, $value){ + return $this->redis->lPushx($this->formatKey($key), $this->formatValue($value)); + } + + /** + * ������������������ ������������������������������ + */ + protected function set_rPush($key, $value){ + return $this->redis->rPush($this->formatKey($key), $this->formatValue($value)); + } + + /** + * ������������������ ������value��������������������������� ������������������������������ + */ + protected function set_rPushx($key, $value){ + return $this->redis->rPushx($this->formatKey($key), $this->formatValue($value)); + } + + /** + * ������������������ + */ + protected function get_lPop($key){ + $value = $this->redis->lPop($this->formatKey($key)); + return $value != FALSE ? $this->unformatValue($value) : NULL; + } + + /** + * ������������������ + */ + protected function get_rPop($key){ + $value = $this->redis->rPop($this->formatKey($key)); + return $value != FALSE ? $this->unformatValue($value) : NULL; + } + + /** + * ������������������ ��������������������������������������� ������������������������������ + */ + protected function get_blPop($key){ + $value = $this->redis->blPop($this->formatKey($key), DI()->config->get('app.redis.blocking')); + return $value != FALSE ? $this->unformatValue($value[1]) : NULL; + } + + /** + * ������������������ ��������������������������������������� ������������������������������ + */ + protected function get_brPop($key){ + $value = $this->redis->brPop($this->formatKey($key), DI()->config->get('app.redis.blocking')); + return $value != FALSE ? $this->unformatValue($value[1]) : NULL; + } + + /** + * ���������key���list������������������ + */ + protected function get_lSize($key){ + return $this->redis->lSize($this->formatKey($key)); + } + + /** + * ���������������key���list������������������������ + */ + protected function set_lSet($key, $index, $value){ + return $this->redis->lSet($this->formatKey($key), $index, $this->formatValue($value)); + } + + /** + * ���������������key���list������������������������ + */ + protected function get_lGet($key, $index){ + $value = $this->redis->lGet($this->formatKey($key), $index); + return $value != FALSE ? $this->unformatValue($value[1]) : NULL; + } + + /** + * ���������������key���list���start���end������������������end��� -1 ������������������ + */ + protected function get_lRange($key, $start, $end){ + $rs = $this->redis->lRange($this->formatKey($key), $start, $end); + foreach($rs as $k => $v){ + $rs[$k] = $this->unformatValue($v); + } + return $rs; + } + + /** + * ���������������key���list���������start���end��������������� + */ + protected function get_lTrim($key, $start, $end){ + $rs = $this->redis->lTrim($this->formatKey($key), $start, $end); + foreach($rs as $k => $v){ + $rs[$k] = $this->unformatValue($v); + } + return $rs; + } + + //��������� lRem lInsert rpoplpush + + //----------------------------------------------------set������--------------------------------------------------- + //----------------------------------------------------zset������--------------------------------------------------- + //----------------------------------------------------Hash������--------------------------------------------------- + + //----------------------------------------------------������������--------------------------------------------------- + /** + * ���������������,������������������ + * @author Axios <axioscros@aliyun.com> + */ + public function counter_forever($key,$dbname=0){ + $this->switchDB($dbname); + if($this->get_exists($key)){ + $count = $this->get_forever($key); + $count++; + $this->set_forever($key,$count); + }else{ + $count = 1; + $this->set_forever($key,$count); + } + + return $count; + } + /** + * ������������������������������������,������������������,������������ms + * @author Axios <axioscros@aliyun.com> + */ + public function counter_time_create($key,$expire = 1000,$dbname=0){ + $this->switchDB($dbname); + $count = 1; + $this->set_time($key,$count,$expire); + $this->redis->pSetEx($this->formatKey($key), $expire, $this->formatValue($count)); + return $count; + } + /** + * ������������������������������������,������������������ + * @author Axios <axioscros@aliyun.com> + */ + public function counter_time_update($key,$dbname=0){ + $this->switchDB($dbname); + if($this->get_exists($key)){ + $count = $this->get_time($key); + $count++; + $expire = $this->redis->pttl($this->formatKey($key)); + $this->set_time($key,$count,$expire); + return $count; + } + return false; + } + /** + * ������������key������������������s��� + */ + protected function setTimeout($key, $time = 600){ + return $this->redis->setTimeout($key, $time); + } + + /** + * ������key������������ + */ + protected function type($key){ + return $this->redis->type($key); + } + + /** + * key���������������unix��������������� + */ + protected function expireAt($key, $time = 600){ + return $this->redis->expireAt($key, $time); + } + + /** + * ������������key���������������key + */ + public function randomKey(){ + return $this->redis->randomKey(); + } + + /** + * ������������������pattern���������key + */ + protected function keys($key, $pattern){ + return $this->redis->keys($key, $pattern); + } + + /** + * ������������������������������key + */ + protected function dbSize(){ + return $this->redis->dbSize(); + } + + /** + * ������������key������������������������ + */ + protected function move($key, $db){ + $arr = DI()->config->get('app.redis.DB'); + $rs = isset($arr[$db]) ? $arr[$db] : $db; + return $this->redis->move($key, $rs); + } + + /** + * ���key��������� + */ + protected function rename($key, $key2){ + + return $this->redis->rename($key, $key2); + } + + /** + * ���key��������� ������������������������������������������������������������ + */ + protected function renameNx($key, $key2){ + return $this->redis->renameNx($key, $key2); + } + + /** + * ������������ ������������������������������(���������������) + */ + protected function del($key){ + return $this->redis->del($this->formatKey($key)); + } + + /** + * ������redis������������������������ + */ + public function info(){ + return $this->redis->info(); + } + + /** + * ������DB������������������������ + */ + public function get_redis(){ + return $this->redis; + } + + /** + * ������������������ + */ + public function ping(){ + return $this->redis->ping(); + } + + /** + * ������������Redis-DB ���������������������DB������������������ + */ + protected function switchDB($name){ + $arr = DI()->config->get('app.redis.DB'); + if(is_int($name)){ + $db = $name; + }else{ + $db = isset($arr[$name]) ? $arr[$name] : 0; + } + if($this->db_old != $db){ + $this->redis->select($db); + $this->db_old = $db; + } + } + //-------------------------------------------------------������������------------------------------------------------ + + /** + * ��������������������� + */ + protected function flushDB(){ + return $this->redis->flushDB(); + } + + /** + * ��������������������� + */ + public function flushAll(){ + return $this->redis->flushAll(); + } + + /** + * ������������������ + */ + public function slaveof($host, $port){ + return $this->redis->slaveof($host, $port); + } + + /** + * ������������������������������ + */ + public function save(){ + return $this->redis->save(); + } + + /** + * ������������������������������ + */ + public function bgsave(){ + return $this->redis->bgsave(); + } + + /** + * ���������������������������������������������Unix������ + */ + public function lastSave(){ + return $this->redis->lastSave(); + } + + /** + * ������aof��������������������������� + */ + protected function bgrewriteaof(){ + return $this->redis->bgrewriteaof(); + } +} \ No newline at end of file diff --git a/Library/Redis/README.md b/Library/Redis/README.md new file mode 100644 index 0000000..d661d2f --- /dev/null +++ b/Library/Redis/README.md @@ -0,0 +1,130 @@ +#������PhalApi���Redis������ + + + +##������## +***������������������phalapi���������������@dogstar,���������������������������������������������������.*** + +���������������������������������������������������������������������redis������������������������������redis��������������������� +,������������phpredis���������������������������������������������������������������������������! + +**���:������������������������������,���������������������������,���������������������������������������������.** + +������: + +������������:[http://www.phalapi.net/](http://www.phalapi.net/ "PhalApi������") + +������������Git������:[http://git.oschina.net/dogstar/PhalApi/tree/release](http://git.oschina.net/dogstar/PhalApi/tree/release "������������Git������") + + +##������������redis������phpredis## + +������centos6.5 + + //���redis������������ + wget http://download.redis.io/releases/redis-2.8.17.tar.gz + tar zxvf redis-2.8.17.tar.gz + cd redis-2.8.17 + make + make test + make install + //������6379������������������������ + cd utils + ./install_server.sh + Please select the redis port for this instance: [6379] + Please select the redis config file name [/etc/redis/6379.conf] + Please select the redis log file name [/var/log/redis_6379.log] + Please select the data directory for this instance [/var/lib/redis/6379] + Please select the redis executable path [/usr/local/bin/redis-server] + //��������������������������� + vi /etc/redis/6379.conf + databases 100 #���������������������������������16���100 + masterauth xxxxxxxxxxxxx #������ master ��������������� + requirepass woyouwaimai76 #���������redis��������������� + :wq + //������������redis������������ + vi /etc/rc.d/init.d/redis_6379 + $CLIEXEC -p $REDISPORT -a woyouwaimai76 shutdown #stop redis������������ + //������redis + service redis_6379 restart + //������������������������ + chkconfig redis_6379 on + + //������phpredis������������ + wget https://github.com/nicolasff/phpredis/archive/master.zip + unzip master.zip -d phpredis + cd phpredis/phpredis-master + phpize + ./configure + make && make install + //���php.ini���������phpredis + extension = redis.so + + //������ + <?php + $auth = 'xxxxxxxxx'; + $source = '127.0.0.1'; + $host = '6379'; + $redis = new Redis(); + echo $redis->connect($host) ? "$host connect" : "$host fail"; + if($auth){ + echo $redis->auth($auth) ? " auth success" : " auth fail"; + } + + +##���������������������Config.app������������## + return array( + //Redis��������� + 'redis' => array( + //Redis��������������� + 'servers' => array( + 'host' => '127.0.0.1', //Redis��������������� + 'port' => '6379', //Redis��������� + 'prefix' => 'developers_', //Redis-key������ + 'auth' => 'woyouwaimai76', //Redis������������ + ), + // Redis������������������ + 'DB' => array( + 'developers' => 1, + 'user' => 2, + 'code' => 3, + ), + //���������������������������������������������������/��� + 'blocking' => 5, + ), + + ); + +##���init������������������redis������## + +������������������������������������������������������������Library���������������,��������������������������������������������� + + //redis������ + DI()->redis = new Redis_Lite(DI()->config->get('app.redis.servers')); + +##������������## + + //������������������������ + DI()->redis->set_forever(������,���,������); + //������������������������ + DI()->redis->get_forever(������, ������); + + //������������������������������������,������600��� + DI()->redis->set_Time(������,���,������������,������); + //������������������������������������ + DI()->redis->get_Time(������, ������); + + //������������������ + DI()->redis->set_Lpush(������������,���, ������); + //������������������ + DI()->redis->get_lpop(������������, ������); + //������������������ ���������������������������������������(���������������������������������blocking������) + DI()->redis->get_Brpop(������������,���, ������); + + //������������������������������������ + DI()->redis->del(������, ������); + //������������ + DI()->redis->get_incr(������, ������); + //������DB������������������������ + DI()->redis->get_redis(������, ������); + \ No newline at end of file diff --git a/Yfs/Api/Data.php b/Yfs/Api/Data.php new file mode 100644 index 0000000..05d742f --- /dev/null +++ b/Yfs/Api/Data.php @@ -0,0 +1,38 @@ +<?php +/** + * ��������������� + */ + +class Api_Data extends PhalApi_Api { + + public function getRules() { + return array( + 'upload' => array( + 'data' => array('name' => 'data', 'type' => 'string' ,'require' => true, 'desc' => '������������'), + ), + ); + } + + /** + * ������������ + * @desc ������������������������������ + * @return bool code ������������0��������������������� 1������������ + * @return string msg ������������ + */ + public function upload(){ + $rs = array('code' => 0, 'msg' => ''); + $data_service = new Domain_Data(); + $data = json_decode($this->data, true); + //print_r(count($data['data']));die(); + if(json_last_error() == JSON_ERROR_NONE){ + $data_service->upload($data); + $rs['msg'] = "������������������"; + } + else{ + $rs['code'] = 1; + $rs['msg'] = "json������������"; + } + return $rs; + } + +} diff --git a/Yfs/Domain/Data.php b/Yfs/Domain/Data.php new file mode 100644 index 0000000..1a39944 --- /dev/null +++ b/Yfs/Domain/Data.php @@ -0,0 +1,35 @@ +<?php +/** + * ��������������� + */ +class Domain_Data { + + public function __construct(){ + //DI()->redis = new Redis_Lite(DI()->config->get('app.redis.servers')); + $this->model = new Model_Data(); + } + + /** + * ������������������������redis + * @return [type] [description] + */ + public function upload($data){ + $arr = array(); + $arr['user_id'] = $data['userid']; + $arr['lon'] = $data['lon']; + $arr['lat'] = $data['lat']; + $arr['address'] = $data['address']; + foreach ($data['data'] as $value) { + $arr['value'] = $value['value']; + $arr['create_time'] = $value['time']; + $this->model->add($arr); + if(isset($arr['_id'])) + unset($arr['_id']); + } + return true; + //$a = rand(1,100); + //DI()->redis->set_lPush('test', $a, 'master'); + + } + +} diff --git a/Yfs/Model/Data.php b/Yfs/Model/Data.php new file mode 100644 index 0000000..155b626 --- /dev/null +++ b/Yfs/Model/Data.php @@ -0,0 +1,8 @@ +<?php + +class Model_Data extends Model_Base{ + + public $table = 'data'; + + +} -- Gitblit v1.8.0