課程
/后端開發(fā)
/PHP
/PHP開發(fā)APP接口
獲取緩存不成功呢
2015-03-30
源自:PHP開發(fā)APP接口 3-1
正在回答
請(qǐng)附上代碼
Mr_Blue_001
<?php
????//調(diào)用文件操作類?? ?// include "./resu.class.php";?? ?//error_reporting(0);?? ?include "file.php";?? ?$data= array(?? ??? ?'id'=>1,?? ??? ?'name'=>'singwa',?? ??? ?'type'=>array(4,5,6),?? ??? ?'test'=>array(1,45,67=>array(1,'45698'),),?? ?);?? ?// resu::show(200,'success',$data,'json');?? ?$file = new File();?? ?if($file->cacheData('index_mk_cache')){?? ??? ?var_dump($file->cacheData('index_mk_cache'));exit();?? ??? ?echo "seccess";?? ?}else{?? ??? ?echo 'error';?? ?}
<?php?? ??? ?class File{?? ??? ?private $_dir;?? ??? ?const EXT = '.txt';?? ??? ?public function __construct() {?? ??? ??? ?$this->_dir = dirname(__FILE__) . '/files/';?? ??? ?}?? ??? ?public function cacheData($key,$value,$path=''){?? ??? ??? ?$filename = $this->_dir? . $key . self::EXT;?? ??? ??? ?if($value != '') { // 將value值寫入緩存?? ??? ??? ??? ?/*if(is_null($value)) {?? ??? ??? ??? ??? ?return @unlink($filename);?? ??? ??? ??? ?}*/?? ??? ??? ?$dir = dirname($filename);?? ??? ??? ??? ?if(!is_dir($dir)) {?? ??? ??? ??? ??? ?mkdir($dir, 0777);?? ??? ??? ??? ?}?? ??? ??? ??? ?return file_put_contents($filename,json_encode($value));?? ??? ??? ?}?? ??? ??? ?if(!is_file($filename)){?? ??? ??? ??? ?return false;?? ??? ??? ?}else{?? ??? ??? ??? ?return json_decode(file_get_contents($filename),true);?? ??? ??? ?}?? ??? ?}?? ?}???
舉報(bào)
APP通信接口技術(shù),不得不掌握的法寶,學(xué)完之后你會(huì)受益良多
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2015-03-30
請(qǐng)附上代碼
2015-03-30
<?php
????//調(diào)用文件操作類
?? ?// include "./resu.class.php";
?? ?//error_reporting(0);
?? ?include "file.php";
?? ?$data= array(
?? ??? ?'id'=>1,
?? ??? ?'name'=>'singwa',
?? ??? ?'type'=>array(4,5,6),
?? ??? ?'test'=>array(1,45,67=>array(1,'45698'),),
?? ?);
?? ?// resu::show(200,'success',$data,'json');
?? ?$file = new File();
?? ?if($file->cacheData('index_mk_cache')){
?? ??? ?var_dump($file->cacheData('index_mk_cache'));exit();
?? ??? ?echo "seccess";
?? ?}else{
?? ??? ?echo 'error';
?? ?}
2015-03-30
<?php
?? ?
?? ?class File{
?? ??? ?private $_dir;
?? ??? ?const EXT = '.txt';
?? ??? ?public function __construct() {
?? ??? ??? ?$this->_dir = dirname(__FILE__) . '/files/';
?? ??? ?}
?? ??? ?public function cacheData($key,$value,$path=''){
?? ??? ??? ?$filename = $this->_dir? . $key . self::EXT;
?? ??? ??? ?if($value != '') { // 將value值寫入緩存
?? ??? ??? ??? ?/*if(is_null($value)) {
?? ??? ??? ??? ??? ?return @unlink($filename);
?? ??? ??? ??? ?}*/
?? ??? ??? ?$dir = dirname($filename);
?? ??? ??? ??? ?if(!is_dir($dir)) {
?? ??? ??? ??? ??? ?mkdir($dir, 0777);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return file_put_contents($filename,json_encode($value));
?? ??? ??? ?}
?? ??? ??? ?if(!is_file($filename)){
?? ??? ??? ??? ?return false;
?? ??? ??? ?}else{
?? ??? ??? ??? ?return json_decode(file_get_contents($filename),true);
?? ??? ??? ?}
?? ??? ?}
?? ?}???