github.com/wangyougui/gf/v2@v2.6.5/os/gsession/gsession_z_example_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 gsession_test
     8  
     9  import (
    10  	"fmt"
    11  	"time"
    12  
    13  	"github.com/wangyougui/gf/v2/container/gmap"
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/os/gctx"
    16  	"github.com/wangyougui/gf/v2/os/gsession"
    17  )
    18  
    19  func ExampleNew() {
    20  	manager := gsession.New(time.Second)
    21  	fmt.Println(manager.GetTTL())
    22  
    23  	// Output:
    24  	// 1s
    25  }
    26  
    27  func ExampleManager_SetStorage() {
    28  	manager := gsession.New(time.Second)
    29  	manager.SetStorage(gsession.NewStorageMemory())
    30  	fmt.Println(manager.GetTTL())
    31  
    32  	// Output:
    33  	// 1s
    34  }
    35  
    36  func ExampleManager_GetStorage() {
    37  	manager := gsession.New(time.Second, gsession.NewStorageMemory())
    38  	size, _ := manager.GetStorage().GetSize(gctx.New(), "id")
    39  	fmt.Println(size)
    40  
    41  	// Output:
    42  	// 0
    43  }
    44  
    45  func ExampleManager_SetTTL() {
    46  	manager := gsession.New(time.Second)
    47  	manager.SetTTL(time.Minute)
    48  	fmt.Println(manager.GetTTL())
    49  
    50  	// Output:
    51  	// 1m0s
    52  }
    53  
    54  func ExampleSession_Set() {
    55  	storage := gsession.NewStorageFile("", time.Second)
    56  	manager := gsession.New(time.Second, storage)
    57  	s := manager.New(gctx.New())
    58  	fmt.Println(s.Set("key", "val") == nil)
    59  
    60  	// Output:
    61  	// true
    62  }
    63  
    64  func ExampleSession_SetMap() {
    65  	storage := gsession.NewStorageFile("", time.Second)
    66  	manager := gsession.New(time.Second, storage)
    67  	s := manager.New(gctx.New())
    68  	fmt.Println(s.SetMap(map[string]interface{}{}) == nil)
    69  
    70  	// Output:
    71  	// true
    72  }
    73  
    74  func ExampleSession_Remove() {
    75  	storage := gsession.NewStorageFile("", time.Second)
    76  	manager := gsession.New(time.Second, storage)
    77  	s1 := manager.New(gctx.New())
    78  	fmt.Println(s1.Remove("key"))
    79  
    80  	s2 := manager.New(gctx.New(), "Remove")
    81  	fmt.Println(s2.Remove("key"))
    82  
    83  	// Output:
    84  	// <nil>
    85  	// <nil>
    86  }
    87  
    88  func ExampleSession_RemoveAll() {
    89  	storage := gsession.NewStorageFile("", time.Second)
    90  	manager := gsession.New(time.Second, storage)
    91  	s1 := manager.New(gctx.New())
    92  	fmt.Println(s1.RemoveAll())
    93  
    94  	s2 := manager.New(gctx.New(), "Remove")
    95  	fmt.Println(s2.RemoveAll())
    96  
    97  	// Output:
    98  	// <nil>
    99  	// <nil>
   100  }
   101  
   102  func ExampleSession_Id() {
   103  	storage := gsession.NewStorageFile("", time.Second)
   104  	manager := gsession.New(time.Second, storage)
   105  	s := manager.New(gctx.New(), "Id")
   106  	id, _ := s.Id()
   107  	fmt.Println(id)
   108  
   109  	// Output:
   110  	// Id
   111  }
   112  
   113  func ExampleSession_SetId() {
   114  	nilSession := &gsession.Session{}
   115  	fmt.Println(nilSession.SetId("id"))
   116  
   117  	storage := gsession.NewStorageFile("", time.Second)
   118  	manager := gsession.New(time.Second, storage)
   119  	s := manager.New(gctx.New())
   120  	s.Id()
   121  	fmt.Println(s.SetId("id"))
   122  
   123  	// Output:
   124  	// <nil>
   125  	// session already started
   126  }
   127  
   128  func ExampleSession_SetIdFunc() {
   129  	nilSession := &gsession.Session{}
   130  	fmt.Println(nilSession.SetIdFunc(func(ttl time.Duration) string {
   131  		return "id"
   132  	}))
   133  
   134  	storage := gsession.NewStorageFile("", time.Second)
   135  	manager := gsession.New(time.Second, storage)
   136  	s := manager.New(gctx.New())
   137  	s.Id()
   138  	fmt.Println(s.SetIdFunc(func(ttl time.Duration) string {
   139  		return "id"
   140  	}))
   141  
   142  	// Output:
   143  	// <nil>
   144  	// session already started
   145  }
   146  
   147  func ExampleSession_Data() {
   148  	storage := gsession.NewStorageFile("", time.Second)
   149  	manager := gsession.New(time.Second, storage)
   150  
   151  	s1 := manager.New(gctx.New())
   152  	data1, _ := s1.Data()
   153  	fmt.Println(data1)
   154  
   155  	s2 := manager.New(gctx.New(), "id_data")
   156  	data2, _ := s2.Data()
   157  	fmt.Println(data2)
   158  
   159  	// Output:
   160  	// map[]
   161  	// map[]
   162  }
   163  
   164  func ExampleSession_Size() {
   165  	storage := gsession.NewStorageFile("", time.Second)
   166  	manager := gsession.New(time.Second, storage)
   167  
   168  	s1 := manager.New(gctx.New())
   169  	size1, _ := s1.Size()
   170  	fmt.Println(size1)
   171  
   172  	s2 := manager.New(gctx.New(), "Size")
   173  	size2, _ := s2.Size()
   174  	fmt.Println(size2)
   175  
   176  	// Output:
   177  	// 0
   178  	// 0
   179  }
   180  
   181  func ExampleSession_Contains() {
   182  	storage := gsession.NewStorageFile("", time.Second)
   183  	manager := gsession.New(time.Second, storage)
   184  
   185  	s1 := manager.New(gctx.New())
   186  	notContains, _ := s1.Contains("Contains")
   187  	fmt.Println(notContains)
   188  
   189  	s2 := manager.New(gctx.New(), "Contains")
   190  	contains, _ := s2.Contains("Contains")
   191  	fmt.Println(contains)
   192  
   193  	// Output:
   194  	// false
   195  	// false
   196  }
   197  
   198  func ExampleStorageFile_SetCryptoKey() {
   199  	storage := gsession.NewStorageFile("", time.Second)
   200  	storage.SetCryptoKey([]byte("key"))
   201  
   202  	size, _ := storage.GetSize(gctx.New(), "id")
   203  	fmt.Println(size)
   204  
   205  	// Output:
   206  	// 0
   207  }
   208  
   209  func ExampleStorageFile_SetCryptoEnabled() {
   210  	storage := gsession.NewStorageFile("", time.Second)
   211  	storage.SetCryptoEnabled(true)
   212  
   213  	size, _ := storage.GetSize(gctx.New(), "id")
   214  	fmt.Println(size)
   215  
   216  	// Output:
   217  	// 0
   218  }
   219  
   220  func ExampleStorageFile_UpdateTTL() {
   221  	var (
   222  		ctx = gctx.New()
   223  	)
   224  
   225  	storage := gsession.NewStorageFile("", time.Second)
   226  	fmt.Println(storage.UpdateTTL(ctx, "id", time.Second*15))
   227  
   228  	time.Sleep(time.Second * 11)
   229  
   230  	// Output:
   231  	// <nil>
   232  }
   233  
   234  func ExampleStorageRedis_Get() {
   235  	storage := gsession.NewStorageRedis(g.Redis())
   236  	val, _ := storage.Get(gctx.New(), "id", "key")
   237  	fmt.Println(val)
   238  
   239  	// May Output:
   240  	// <nil>
   241  }
   242  
   243  func ExampleStorageRedis_Data() {
   244  	storage := gsession.NewStorageRedis(g.Redis())
   245  	val, _ := storage.Data(gctx.New(), "id")
   246  	fmt.Println(val)
   247  
   248  	// May Output:
   249  	// map[]
   250  }
   251  
   252  func ExampleStorageRedis_GetSize() {
   253  	storage := gsession.NewStorageRedis(g.Redis())
   254  	val, _ := storage.GetSize(gctx.New(), "id")
   255  	fmt.Println(val)
   256  
   257  	// May Output:
   258  	// 0
   259  }
   260  
   261  func ExampleStorageRedis_Remove() {
   262  	storage := gsession.NewStorageRedis(g.Redis())
   263  	err := storage.Remove(gctx.New(), "id", "key")
   264  	fmt.Println(err != nil)
   265  
   266  	// May Output:
   267  	// true
   268  }
   269  
   270  func ExampleStorageRedis_RemoveAll() {
   271  	storage := gsession.NewStorageRedis(g.Redis())
   272  	err := storage.RemoveAll(gctx.New(), "id")
   273  	fmt.Println(err != nil)
   274  
   275  	// May Output:
   276  	// true
   277  }
   278  
   279  func ExampleStorageRedis_UpdateTTL() {
   280  	storage := gsession.NewStorageRedis(g.Redis())
   281  	err := storage.UpdateTTL(gctx.New(), "id", time.Second*15)
   282  	fmt.Println(err)
   283  
   284  	time.Sleep(time.Second * 11)
   285  
   286  	// May Output:
   287  	// <nil>
   288  }
   289  
   290  func ExampleStorageRedisHashTable_Get() {
   291  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   292  
   293  	v, err := storage.Get(gctx.New(), "id", "key")
   294  
   295  	fmt.Println(v)
   296  	fmt.Println(err)
   297  
   298  	// May Output:
   299  	// <nil>
   300  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   301  }
   302  
   303  func ExampleStorageRedisHashTable_Data() {
   304  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   305  
   306  	data, err := storage.Data(gctx.New(), "id")
   307  
   308  	fmt.Println(data)
   309  	fmt.Println(err)
   310  
   311  	// May Output:
   312  	// map[]
   313  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   314  }
   315  
   316  func ExampleStorageRedisHashTable_GetSize() {
   317  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   318  
   319  	size, err := storage.GetSize(gctx.New(), "id")
   320  
   321  	fmt.Println(size)
   322  	fmt.Println(err)
   323  
   324  	// May Output:
   325  	// 0
   326  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   327  }
   328  
   329  func ExampleStorageRedisHashTable_Remove() {
   330  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   331  
   332  	err := storage.Remove(gctx.New(), "id", "key")
   333  
   334  	fmt.Println(err)
   335  
   336  	// May Output:
   337  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   338  }
   339  
   340  func ExampleStorageRedisHashTable_RemoveAll() {
   341  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   342  
   343  	err := storage.RemoveAll(gctx.New(), "id")
   344  
   345  	fmt.Println(err)
   346  
   347  	// May Output:
   348  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   349  }
   350  
   351  func ExampleStorageRedisHashTable_GetSession() {
   352  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   353  	data, err := storage.GetSession(gctx.New(), "id", time.Second)
   354  
   355  	fmt.Println(data)
   356  	fmt.Println(err)
   357  
   358  	// May Output:
   359  	//
   360  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   361  }
   362  
   363  func ExampleStorageRedisHashTable_SetSession() {
   364  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   365  
   366  	strAnyMap := gmap.StrAnyMap{}
   367  
   368  	err := storage.SetSession(gctx.New(), "id", &strAnyMap, time.Second)
   369  
   370  	fmt.Println(err)
   371  
   372  	// May Output:
   373  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   374  }
   375  
   376  func ExampleStorageRedisHashTable_UpdateTTL() {
   377  	storage := gsession.NewStorageRedisHashTable(g.Redis())
   378  
   379  	err := storage.UpdateTTL(gctx.New(), "id", time.Second)
   380  
   381  	fmt.Println(err)
   382  
   383  	// May Output:
   384  	// redis adapter is not set, missing configuration or adapter register? possible reference: https://github.com/wangyougui/gf/tree/master/contrib/nosql/redis
   385  }