From 1f157cf60289251b4fc426cb9353f4142bf36fde Mon Sep 17 00:00:00 2001 From: john Date: Fri, 14 Sep 2018 17:04:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89gfilepool=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=B1=A0=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gfile/gfile.go | 2 +- g/os/gfilepool/gfilepool.go | 83 ------------------------------------- 2 files changed, 1 insertion(+), 84 deletions(-) delete mode 100644 g/os/gfilepool/gfilepool.go diff --git a/g/os/gfile/gfile.go b/g/os/gfile/gfile.go index f5a220d1f..fb2b4384a 100644 --- a/g/os/gfile/gfile.go +++ b/g/os/gfile/gfile.go @@ -381,7 +381,7 @@ func putContents(path string, data []byte, flag int, perm os.FileMode) error { return err } } - // 创建/打开文件,使用文件指针池,默认为60秒 + // 创建/打开文件 f, err := os.OpenFile(path, flag, perm) if err != nil { return err diff --git a/g/os/gfilepool/gfilepool.go b/g/os/gfilepool/gfilepool.go deleted file mode 100644 index 581e1a191..000000000 --- a/g/os/gfilepool/gfilepool.go +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://gitee.com/johng/gf. - -// 文件指针池 -package gfilepool - -import ( - "os" - "sync" - "gitee.com/johng/gf/g/container/gmap" - "gitee.com/johng/gf/g/container/gpool" - "fmt" -) - -// 文件指针池 -type Pool struct { - pool *gpool.Pool // 底层对象池 -} - -// 文件指针池指针 -type File struct { - *os.File // 底层文件指针 - mu sync.RWMutex // 互斥锁 - pool *Pool // 所属池 -} - -// 全局指针池,expire < 0表示不过期,expire = 0表示使用完立即回收,expire > 0表示超时回收 -var pools = gmap.NewStringInterfaceMap() - -// 获得文件对象,并自动创建指针池 -func OpenWithPool(path string, flag int, perm os.FileMode, expire int) (*File, error) { - key := fmt.Sprintf("%s&%d&%d&%d", path, flag, expire, perm) - result := pools.Get(key) - if result != nil { - return result.(*Pool).File() - } - pool := New(path, flag, perm, expire) - pools.Set(key, pool) - return pool.File() -} - -// 创建一个文件指针池,expire = 0表示不过期,expire < 0表示使用完立即回收,expire > 0表示超时回收 -func New(path string, flag int, perm os.FileMode, expire int) *Pool { - p := &Pool {} - p.pool = gpool.New(expire, func() (interface{}, error) { - file, err := os.OpenFile(path, flag, perm) - if err != nil { - return nil, err - } - return &File{ - File : file, - pool : p, - }, nil - }) - p.pool.SetExpireFunc(func(i interface{}) { - i.(*File).File.Close() - }) - return p -} - -// 获得一个文件打开指针 -func (p *Pool) File() (*File, error) { - if v, err := p.pool.Get(); err != nil { - return nil, err - } else { - return v.(*File), nil - } -} - -// 关闭指针池(返回error是标准库io.ReadWriteCloser接口实现) -func (p *Pool) Close() error { - p.pool.Close() - return nil -} - -// 获得底层文件指针(返回error是标准库io.ReadWriteCloser接口实现) -func (f *File) Close() error { - f.pool.pool.Put(f) - return nil -}