github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_z_example_cache_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gfile_test
     8  
     9  import (
    10  	"fmt"
    11  	"time"
    12  
    13  	"github.com/wangyougui/gf/v2/os/gfile"
    14  )
    15  
    16  func ExampleGetContentsWithCache() {
    17  	// init
    18  	var (
    19  		fileName = "gfile_example.txt"
    20  		tempDir  = gfile.Temp("gfile_example_cache")
    21  		tempFile = gfile.Join(tempDir, fileName)
    22  	)
    23  
    24  	// write contents
    25  	gfile.PutContents(tempFile, "goframe example content")
    26  
    27  	// It reads the file content with cache duration of one minute,
    28  	// which means it reads from cache after then without any IO operations within on minute.
    29  	fmt.Println(gfile.GetContentsWithCache(tempFile, time.Minute))
    30  
    31  	// write new contents will clear its cache
    32  	gfile.PutContents(tempFile, "new goframe example content")
    33  
    34  	// There's some delay for cache clearing after file content change.
    35  	time.Sleep(time.Second * 1)
    36  
    37  	// read contents
    38  	fmt.Println(gfile.GetContentsWithCache(tempFile))
    39  
    40  	// May Output:
    41  	// goframe example content
    42  	// new goframe example content
    43  }