github.com/gogf/gf/v2@v2.7.4/os/gcache/gcache_cache_must.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/gogf/gf.
     6  
     7  package gcache
     8  
     9  import (
    10  	"context"
    11  	"time"
    12  
    13  	"github.com/gogf/gf/v2/container/gvar"
    14  )
    15  
    16  // MustGet acts like Get, but it panics if any error occurs.
    17  func (c *Cache) MustGet(ctx context.Context, key interface{}) *gvar.Var {
    18  	v, err := c.Get(ctx, key)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	return v
    23  }
    24  
    25  // MustGetOrSet acts like GetOrSet, but it panics if any error occurs.
    26  func (c *Cache) MustGetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) *gvar.Var {
    27  	v, err := c.GetOrSet(ctx, key, value, duration)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	return v
    32  }
    33  
    34  // MustGetOrSetFunc acts like GetOrSetFunc, but it panics if any error occurs.
    35  func (c *Cache) MustGetOrSetFunc(ctx context.Context, key interface{}, f Func, duration time.Duration) *gvar.Var {
    36  	v, err := c.GetOrSetFunc(ctx, key, f, duration)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	return v
    41  }
    42  
    43  // MustGetOrSetFuncLock acts like GetOrSetFuncLock, but it panics if any error occurs.
    44  func (c *Cache) MustGetOrSetFuncLock(ctx context.Context, key interface{}, f Func, duration time.Duration) *gvar.Var {
    45  	v, err := c.GetOrSetFuncLock(ctx, key, f, duration)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	return v
    50  }
    51  
    52  // MustContains acts like Contains, but it panics if any error occurs.
    53  func (c *Cache) MustContains(ctx context.Context, key interface{}) bool {
    54  	v, err := c.Contains(ctx, key)
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  	return v
    59  }
    60  
    61  // MustGetExpire acts like GetExpire, but it panics if any error occurs.
    62  func (c *Cache) MustGetExpire(ctx context.Context, key interface{}) time.Duration {
    63  	v, err := c.GetExpire(ctx, key)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  	return v
    68  }
    69  
    70  // MustSize acts like Size, but it panics if any error occurs.
    71  func (c *Cache) MustSize(ctx context.Context) int {
    72  	v, err := c.Size(ctx)
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  	return v
    77  }
    78  
    79  // MustData acts like Data, but it panics if any error occurs.
    80  func (c *Cache) MustData(ctx context.Context) map[interface{}]interface{} {
    81  	v, err := c.Data(ctx)
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  	return v
    86  }
    87  
    88  // MustKeys acts like Keys, but it panics if any error occurs.
    89  func (c *Cache) MustKeys(ctx context.Context) []interface{} {
    90  	v, err := c.Keys(ctx)
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	return v
    95  }
    96  
    97  // MustKeyStrings acts like KeyStrings, but it panics if any error occurs.
    98  func (c *Cache) MustKeyStrings(ctx context.Context) []string {
    99  	v, err := c.KeyStrings(ctx)
   100  	if err != nil {
   101  		panic(err)
   102  	}
   103  	return v
   104  }
   105  
   106  // MustValues acts like Values, but it panics if any error occurs.
   107  func (c *Cache) MustValues(ctx context.Context) []interface{} {
   108  	v, err := c.Values(ctx)
   109  	if err != nil {
   110  		panic(err)
   111  	}
   112  	return v
   113  }