月度归档: 2019 年 4 月

  • PHP日志类

    新建Log.class.php,复制以下代码放入其中:

    <?php
    interface ILogHandler {
        public function write($msg);
    }
    
    // PHP日志类
    class CLogFileHandler implements ILogHandler {
        private $handle = null;
    
        public function __construct($file = '') {
            $this->handle = fopen($file, 'a');
        }
    
        public function write($msg) {
            fwrite($this->handle, $msg, 4096);
        }
    
        public function __destruct() {
            fclose($this->handle);
        }
    }
    class Log {
        private $handler = null;
        private $level = 15;
    
        private static $instance = null;
    
        private function __construct() {
        }
        private function __clone() {
        }
    
        public static function Init($handler = null, $level = 15) {
            if (!self::$instance instanceof self) {
                self::$instance = new self();
                self::$instance->__setHandle($handler);
                self::$instance->__setLevel($level);
            }
            return self::$instance;
        }
    
    
        private function __setHandle($handler) {
            $this->handler = $handler;
        }
    
        private function __setLevel($level) {
            $this->level = $level;
        }
    
        public static function DEBUG($msg) {
            self::$instance->write(1, $msg);
        }
    
        public static function WARN($msg) {
            self::$instance->write(4, $msg);
        }
    
        public static function ERROR($msg) {
            $debugInfo = debug_backtrace();
            $stack = "[";
            foreach ($debugInfo as $key => $val) {
                if (array_key_exists("file", $val)) {
                    $stack .= ",file:" . $val["file"];
                }
                if (array_key_exists("line", $val)) {
                    $stack .= ",line:" . $val["line"];
                }
                if (array_key_exists("function", $val)) {
                    $stack .= ",function:" . $val["function"];
                }
            }
            $stack .= "]";
            self::$instance->write(8, $stack . $msg);
        }
    
        public static function INFO($msg) {
            self::$instance->write(2, $msg);
        }
    
        private function getLevelStr($level) {
            switch ($level) {
                case 1:
                    return 'debug';
                    break;
                case 2:
                    return 'info';
                    break;
                case 4:
                    return 'warn';
                    break;
                case 8:
                    return 'error';
                    break;
                default:
            }
        }
    
        protected function write($level, $msg) {
            if (($level & $this->level) == $level) {
                $msg = '[' . date('Y-m-d H:i:s') . '][' . $this->getLevelStr($level) . '] ' . $msg . "\n";
                $this->handler->write($msg);
            }
        }
    }

    调用方式:

    <?php
    require 'Log.class.php'; // 导入日志类文件
    define('DS', DIRECTORY_SEPARATOR); // 设置目录分隔符
    define('LOG_PATH', dirname(__FILE__) . DS . 'log' . DS); // 日志文件目录
    
    $logHandler = new CLogFileHandler(LOG_PATH . date('Y-m-d') . '.log');
    $log = Log::Init($logHandler, 15);
    
    Log::DEBUG('111111');
  • 更新laravel框架

    composer update laravel/framework

  • centos 编译安装apache httpd

    wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.39.tar.gz
    wget http://mirror.bit.edu.cn/apache/apr/apr-1.7.0.tar.gz
    wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

    解压缩:

    tar -xvf httpd-2.4.39.tar.gz
    tar -xvf apr-1.7.0.tar.gz
    tar -xvf apr-util-1.6.1.tar.gz

    一点文件操作:

    mv apr-1.7.0 httpd-2.4.39/srclib/apr
    mv apr-util-1.6.1 httpd-2.4.39/srclib/apr-util
    cd httpd-2.4.39

    编译前安装 :

    yum -y install gcc make zlib-devel pcre-devel expat-devel
    
    mkdir /web
    
    mkdir /web/httpd
    
    mkdir  /web/php

    编译命令:

    ./configure --prefix=/web/httpd --with-included-apr --enable-nonportable-atomics=yes --with-z

    编译完成后:

    make
    
    make install
    
    service httpd start
  • ASP.NET CORE中的IOC注册类的三种方式

    AddSingleton():

    When we use the AddSingleton() method to register a service, then it will create a singleton service. It means a single instance of that service is created and that singleton instance is shared among all the components of the application that require it. That singleton service is created when we requested for the first time.

    AddScoped():

    Scoped means instance per request. When we use the AddScoped() method to register a service, then it will create a Scoped service. It means, an instance of the service is created once per each HTTP request and uses that instance in other calls of the same request.

    AddTransient():

    When we use the AddTransient() method to register a service, then it will create a Transient service. It means a new instance of the specified service is created each time when it is requested and they are never shared.

    Note: In the real-time applications, you need to register the components such as application-wide configuration as Singleton. The Database access classes like Entity Framework contexts are recommended to be registered as Scoped so that the connection can be re-used. If you want to run anything in parallel then it is better to register the component as Transient.


    Singleton:每次获取的时候,都是同一个实例,相当于静态类

    生命周期:项目启动——项目关闭

    AddSingleton可以直接用圆括号添加一个实例

    Transient: 每次从容器中获取的时候,都是一个新的实例

    生命周期:GC回收,主动释放

    Scoped: 对象在请求是才开始创建,在这次请求中获取的对象都是同一个

    生命周期:请求开始——请求结束

    scoped在同一次请求中,获取多次对象得到的是同一个对象

    对于同一个接口的多个实现或静态类,最后注册的实现可替换之前的