[feature] improve code gfile gcfg

This commit is contained in:
houseme
2021-11-15 20:26:31 +08:00
parent 485785e023
commit b664982be3
50 changed files with 110 additions and 96 deletions

View File

@ -11,8 +11,9 @@ package gcache
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
"time"
"github.com/gogf/gf/v2/container/gvar"
)
// Default cache object.

View File

@ -8,8 +8,9 @@ package gcache
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
"time"
"github.com/gogf/gf/v2/container/gvar"
)
// Adapter is the core adapter for cache features implements.

View File

@ -8,13 +8,13 @@ package gcache
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
"math"
"time"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gtimer"
)

View File

@ -7,9 +7,10 @@
package gcache
import (
"github.com/gogf/gf/v2/os/gtime"
"sync"
"time"
"github.com/gogf/gf/v2/os/gtime"
)
type adapterMemoryData struct {

View File

@ -7,8 +7,9 @@
package gcache
import (
"github.com/gogf/gf/v2/container/gset"
"sync"
"github.com/gogf/gf/v2/container/gset"
)
type adapterMemoryExpireSets struct {

View File

@ -14,8 +14,6 @@ import (
func (item *adapterMemoryItem) IsExpired() bool {
// Note that it should use greater than or equal judgement here
// imagining that the cache time is only 1 millisecond.
if item.e >= gtime.TimestampMilli() {
return false
}
return true
return item.e < gtime.TimestampMilli()
}

View File

@ -72,12 +72,12 @@ func (lru *adapterMemoryLru) Pop() interface{} {
}
// Print is used for test only.
//func (lru *adapterMemoryLru) Print() {
// func (lru *adapterMemoryLru) Print() {
// for _, v := range lru.list.FrontAll() {
// fmt.Printf("%v ", v)
// }
// fmt.Println()
//}
// }
// SyncAndClear synchronizes the keys from `rawList` to `list` and `data`
// using Least Recently Used algorithm.

View File

@ -8,9 +8,10 @@ package gcache
import (
"context"
"time"
"github.com/gogf/gf/v2/os/gtimer"
"github.com/gogf/gf/v2/util/gconv"
"time"
)
// Cache struct.

View File

@ -8,8 +8,9 @@ package gcache
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
"time"
"github.com/gogf/gf/v2/container/gvar"
)
// MustGet acts like Get, but it panics if any error occurs.

View File

@ -2,9 +2,10 @@ package gcache_test
import (
"fmt"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcache"
"time"
)
func ExampleNew() {

View File

@ -10,7 +10,6 @@ package gcache_test
import (
"context"
"github.com/gogf/gf/v2/util/guid"
"math"
"testing"
"time"
@ -20,6 +19,7 @@ import (
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/grpool"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)
var (
@ -458,7 +458,7 @@ func TestCache_SetConcurrency(t *testing.T) {
}()
select {
case <-time.After(2 * time.Second):
//t.Log("first part end")
// t.Log("first part end")
}
go func() {
@ -470,7 +470,7 @@ func TestCache_SetConcurrency(t *testing.T) {
}()
select {
case <-time.After(2 * time.Second):
//t.Log("second part end")
// t.Log("second part end")
}
})
}

View File

@ -10,6 +10,7 @@ package gcfg
import (
"context"
"fmt"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/internal/intlog"

View File

@ -8,6 +8,7 @@ package gcfg
import (
"context"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gvar"

View File

@ -8,6 +8,7 @@ package gcfg
import (
"context"
"github.com/gogf/gf/v2/internal/intlog"
)

View File

@ -10,6 +10,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"

View File

@ -9,9 +9,10 @@
package gcfg_test
import (
"testing"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/test/gtest"
"testing"
)
func TestAdapterFile_SetPath(t *testing.T) {

View File

@ -9,14 +9,13 @@
package gcfg_test
import (
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/genv"
"testing"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/genv"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
)

View File

@ -10,12 +10,13 @@ package gcfg
import (
"context"
"testing"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/os/genv"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"testing"
)
var (

View File

@ -9,10 +9,11 @@
package gcmd
import (
"os"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/internal/command"
"github.com/gogf/gf/v2/internal/utils"
"os"
)
var (

View File

@ -8,17 +8,15 @@
package gcmd
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
"os"
"strings"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
// Parser for arguments.

View File

@ -9,12 +9,11 @@
package gcmd_test
import (
"github.com/gogf/gf/v2/os/genv"
"testing"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/genv"
"github.com/gogf/gf/v2/test/gtest"
)

View File

@ -13,9 +13,7 @@ import (
"testing"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/test/gtest"
)

View File

@ -9,9 +9,9 @@ package gcron
import (
"context"
"github.com/gogf/gf/v2/os/glog"
"time"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/os/gtimer"
)

View File

@ -8,13 +8,13 @@ package gcron
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"reflect"
"runtime"
"time"
"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtimer"
"github.com/gogf/gf/v2/util/gconv"
)

View File

@ -7,13 +7,13 @@
package gcron
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
"strconv"
"strings"
"time"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gregex"
)

View File

@ -8,11 +8,11 @@ package gcron_test
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"testing"
"time"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/test/gtest"
)
@ -52,7 +52,7 @@ func TestCron_Basic(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
cron := gcron.New()
cron.Add(ctx, "* * * * * *", func(ctx context.Context) {}, "add")
//fmt.Println("start", time.Now())
// fmt.Println("start", time.Now())
cron.DelayAdd(ctx, time.Second, "* * * * * *", func(ctx context.Context) {}, "delay_add")
t.Assert(cron.Size(), 1)
time.Sleep(1200 * time.Millisecond)

View File

@ -8,11 +8,11 @@ package gcron_test
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"testing"
"time"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/test/gtest"
)

View File

@ -8,11 +8,12 @@
package genv
import (
"os"
"strings"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/internal/utils"
"github.com/gogf/gf/v2/os/gcmd"
"os"
"strings"
)
// All returns a copy of strings representing the environment,

View File

@ -7,11 +7,11 @@
package genv_test
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"os"
"testing"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/genv"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"

View File

@ -8,7 +8,6 @@
package gfile
import (
"github.com/gogf/gf/v2/text/gstr"
"os"
"os/exec"
"path/filepath"
@ -16,6 +15,7 @@ import (
"time"
"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)

View File

@ -8,11 +8,12 @@ package gfile
import (
"context"
"time"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gfsnotify"
"time"
)
const (

View File

@ -7,12 +7,13 @@
package gfile
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
// Copy file/directory from `src` to `dst`.

View File

@ -8,13 +8,14 @@ package gfile
import (
"bytes"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"os"
"os/exec"
"os/user"
"runtime"
"strings"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
// Home returns absolute path of current user's home directory.

View File

@ -7,12 +7,13 @@
package gfile
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gstr"
"os"
"path/filepath"
"sort"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gstr"
)
const (

View File

@ -9,10 +9,10 @@ package gfile
import (
"bytes"
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
// Search searches file by name `name` in following paths with priority:

View File

@ -7,12 +7,12 @@
package gfile
import (
"github.com/gogf/gf/v2/text/gstr"
"os"
"runtime"
"strings"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
var (

View File

@ -7,12 +7,13 @@
package gfile_test
import (
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"io/ioutil"
"os"
"testing"
"time"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_GetContentsWithCache(t *testing.T) {

View File

@ -13,10 +13,9 @@ import (
"strings"
"testing"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)
func createTestFile(filename, content string) error {

View File

@ -7,10 +7,11 @@
package gfile_test
import (
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"testing"
)
func Test_Copy(t *testing.T) {

View File

@ -7,10 +7,10 @@
package gfile_test
import (
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/errors/gerror"
"testing"
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)

View File

@ -7,12 +7,11 @@
package gfile_test
import (
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/debug/gdebug"
"testing"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)

View File

@ -7,11 +7,11 @@
package gfile_test
import (
"github.com/gogf/gf/v2/util/gconv"
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
)
func Test_Size(t *testing.T) {

View File

@ -12,11 +12,10 @@ import (
"strings"
"testing"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
)
func Test_IsDir(t *testing.T) {
@ -399,7 +398,7 @@ func Test_Glob(t *testing.T) {
testpath() + "/testfiles/t2.txt",
}
//===============================构建测试文件
// ===============================构建测试文件
createDir(dirpath)
for _, v := range havelist1 {
createTestFile(dirpath+"/"+v, "")

View File

@ -8,14 +8,15 @@
package gfpool
import (
"os"
"time"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gpool"
"github.com/gogf/gf/v2/container/gtype"
"os"
"time"
)
// File pointer pool.
// Pool pointer pool.
type Pool struct {
id *gtype.Int // Pool id, which is used to mark this pool whether recreated.
pool *gpool.Pool // Underlying pool.

View File

@ -8,10 +8,11 @@ package gfpool
import (
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"os"
"time"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
// Open creates and returns a file item with given file path, flag and opening permission.
@ -25,10 +26,10 @@ func Open(path string, flag int, perm os.FileMode, ttl ...time.Duration) (file *
// DO NOT search the path here wasting performance!
// Leave following codes just for warning you.
//
//path, err = gfile.Search(path)
//if err != nil {
// path, err = gfile.Search(path)
// if err != nil {
// return nil, err
//}
// }
pool := pools.GetOrSetFuncLock(
fmt.Sprintf("%s&%d&%d&%d", path, flag, fpTTL, perm),
func() interface{} {

View File

@ -7,13 +7,14 @@
package gfpool_test
import (
"os"
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gfpool"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
"os"
"testing"
)
func Test_ConcurrentOS(t *testing.T) {
@ -97,7 +98,7 @@ func Test_ConcurrentOS(t *testing.T) {
t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
})
// DATA RACE
//gtest.C(t, func(t *gtest.T) {
// gtest.C(t, func(t *gtest.T) {
// path := gfile.TempDir(gtime.TimestampNanoStr())
// defer gfile.Remove(path)
// f1, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
@ -131,7 +132,7 @@ func Test_ConcurrentOS(t *testing.T) {
// close(ch)
// wg.Wait()
// t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
//})
// })
}
func Test_ConcurrentGFPool(t *testing.T) {
@ -157,7 +158,7 @@ func Test_ConcurrentGFPool(t *testing.T) {
t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
})
// DATA RACE
//gtest.C(t, func(t *gtest.T) {
// gtest.C(t, func(t *gtest.T) {
// path := gfile.TempDir(gtime.TimestampNanoStr())
// defer gfile.Remove(path)
// f1, err := gfpool.Open(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0666)
@ -191,5 +192,5 @@ func Test_ConcurrentGFPool(t *testing.T) {
// close(ch)
// wg.Wait()
// t.Assert(gstr.Count(gfile.GetContents(path), "@1234567890#"), 2000)
//})
// })
}

View File

@ -9,10 +9,6 @@ package gfsnotify
import (
"context"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
"sync"
"time"
@ -20,7 +16,11 @@ import (
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gqueue"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gcache"
)

View File

@ -8,11 +8,11 @@ package gfsnotify
import (
"context"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/container/glist"
)
// Add monitors `path` with callback function `callbackFunc` to the watcher.

View File

@ -8,6 +8,7 @@ package gfsnotify
import (
"context"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/internal/intlog"
)

View File

@ -7,10 +7,10 @@
package gfsnotify_test
import (
"github.com/gogf/gf/v2/container/garray"
"testing"
"time"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gfsnotify"
@ -204,7 +204,7 @@ func TestWatcher_WatchFolderWithoutRecursively(t *testing.T) {
t.AssertNil(err)
_, err = gfsnotify.Add(dirPath, func(event *gfsnotify.Event) {
//fmt.Println(event.String())
// fmt.Println(event.String())
array.Append(1)
}, false)
t.AssertNil(err)