zoukankan      html  css  js  c++  java
  • PHP开发APP接口(四)

    核心技术:
    缓存技术、定时任务

    1静态缓存,
    2.Memcache redis缓存
    使用缓存减小服务器压力

    静态缓存保存在磁盘上的静态文件
    PHP操作缓存:生成缓存、获取缓存、删除缓存;

    <?php

    class File(){
        private $_dir;
        const EXT =".txt";
        public function __construct(){
            $this->_dir=dirname(__FILE__)."/files/";
        }
    /**
    *按综合方式输出通信数据
    *@param string $key 文件名
    *@param string $value 数据
    *@param string $path 路径
    *@return string
    */
        public function cacheData($key,$value='',$path=''){
                $filename=$this->_dir.$path.$key.self::EXT;

                if($value !== ""){
                    if(is_null($value)){
                        return @unlink($filename);
                    }
                    //将value值写入缓存
                    $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);
                }
        }
    }
    ?>

    ============================================================================

    <?php
    /*上面的函数,$value为空的时候就是获取缓存,不为空就是写入,
    为NULL就是删除缓存*/
    $file = new File();
    if($file->cacheData('index_mk_cache',null)){
        echo "success";
    }else{
        echo "error";
    }

    ?>

  • 相关阅读:
    Will Go eventually replace C++ as Google hoped when Go came out?
    5G到底什么时候来,它究竟能给我们带来什么?
    eog——Eye of GNOME Image Viewer
    Appweb——Embedded Web Server
    【2017】数字重排
    【9203】众数
    【2034】四人投票
    【9204】第k小整数
    【2031】求一元三次方程的解
    iOS 7: 如何为iPhone 5s编译64位应用
  • 原文地址:https://www.cnblogs.com/eis13/p/5555860.html
Copyright © 2011-2022 走看看