github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/cache.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package setting
     7  
     8  import (
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/gitbundle/modules/log"
    13  )
    14  
    15  // Cache represents cache settings
    16  type Cache struct {
    17  	Enabled  bool
    18  	Adapter  string
    19  	Interval int
    20  	Conn     string
    21  	TTL      time.Duration `ini:"ITEM_TTL"`
    22  }
    23  
    24  // CacheService the global cache
    25  var CacheService = struct {
    26  	Cache `ini:"cache"`
    27  
    28  	LastCommit struct {
    29  		Enabled      bool
    30  		TTL          time.Duration `ini:"ITEM_TTL"`
    31  		CommitsCount int64
    32  	} `ini:"cache.last_commit"`
    33  }{
    34  	Cache: Cache{
    35  		Enabled:  true,
    36  		Adapter:  "memory",
    37  		Interval: 60,
    38  		TTL:      16 * time.Hour,
    39  	},
    40  	LastCommit: struct {
    41  		Enabled      bool
    42  		TTL          time.Duration `ini:"ITEM_TTL"`
    43  		CommitsCount int64
    44  	}{
    45  		Enabled:      true,
    46  		TTL:          8760 * time.Hour,
    47  		CommitsCount: 1000,
    48  	},
    49  }
    50  
    51  // MemcacheMaxTTL represents the maximum memcache TTL
    52  const MemcacheMaxTTL = 30 * 24 * time.Hour
    53  
    54  func newCacheService() {
    55  	sec := Cfg.Section("cache")
    56  	if err := sec.MapTo(&CacheService); err != nil {
    57  		log.Fatal("Failed to map Cache settings: %v", err)
    58  	}
    59  
    60  	CacheService.Adapter = sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache", "twoqueue"})
    61  	switch CacheService.Adapter {
    62  	case "memory":
    63  	case "redis", "memcache":
    64  		CacheService.Conn = strings.Trim(sec.Key("HOST").String(), "\" ")
    65  	case "twoqueue":
    66  		CacheService.Conn = strings.TrimSpace(sec.Key("HOST").String())
    67  		if CacheService.Conn == "" {
    68  			CacheService.Conn = "50000"
    69  		}
    70  	case "": // disable cache
    71  		CacheService.Enabled = false
    72  	default:
    73  		log.Fatal("Unknown cache adapter: %s", CacheService.Adapter)
    74  	}
    75  
    76  	if CacheService.Enabled {
    77  		log.Info("Cache Service Enabled")
    78  	} else {
    79  		log.Warn("Cache Service Disabled so that captcha disabled too")
    80  		// captcha depends on cache service
    81  		Service.EnableCaptcha = false
    82  	}
    83  
    84  	sec = Cfg.Section("cache.last_commit")
    85  	if !CacheService.Enabled {
    86  		CacheService.LastCommit.Enabled = false
    87  	}
    88  
    89  	CacheService.LastCommit.CommitsCount = sec.Key("COMMITS_COUNT").MustInt64(1000)
    90  
    91  	if CacheService.LastCommit.Enabled {
    92  		log.Info("Last Commit Cache Service Enabled")
    93  	}
    94  }
    95  
    96  // TTLSeconds returns the TTLSeconds or unix timestamp for memcache
    97  func (c Cache) TTLSeconds() int64 {
    98  	if c.Adapter == "memcache" && c.TTL > MemcacheMaxTTL {
    99  		return time.Now().Add(c.TTL).Unix()
   100  	}
   101  	return int64(c.TTL.Seconds())
   102  }
   103  
   104  // LastCommitCacheTTLSeconds returns the TTLSeconds or unix timestamp for memcache
   105  func LastCommitCacheTTLSeconds() int64 {
   106  	if CacheService.Adapter == "memcache" && CacheService.LastCommit.TTL > MemcacheMaxTTL {
   107  		return time.Now().Add(CacheService.LastCommit.TTL).Unix()
   108  	}
   109  	return int64(CacheService.LastCommit.TTL.Seconds())
   110  }