summaryrefslogtreecommitdiff
path: root/internal/cache/emojicategory.go
blob: 17df5591a8687aeae0a5ebc280e30248bf0e0bad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
   GoToSocial
   Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package cache

import (
	"strings"
	"time"

	"codeberg.org/gruf/go-cache/v2"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)

// EmojiCategoryCache is a cache wrapper to provide ID lookups for gtsmodel.EmojiCategory
type EmojiCategoryCache struct {
	cache cache.LookupCache[string, string, *gtsmodel.EmojiCategory]
}

// NewEmojiCategoryCache returns a new instantiated EmojiCategoryCache object
func NewEmojiCategoryCache() *EmojiCategoryCache {
	c := &EmojiCategoryCache{}
	c.cache = cache.NewLookup(cache.LookupCfg[string, string, *gtsmodel.EmojiCategory]{
		RegisterLookups: func(lm *cache.LookupMap[string, string]) {
			lm.RegisterLookup("name")
		},

		AddLookups: func(lm *cache.LookupMap[string, string], emojiCategory *gtsmodel.EmojiCategory) {
			lm.Set(("name"), strings.ToLower(emojiCategory.Name), emojiCategory.ID)
		},

		DeleteLookups: func(lm *cache.LookupMap[string, string], emojiCategory *gtsmodel.EmojiCategory) {
			lm.Delete("name", strings.ToLower(emojiCategory.Name))
		},
	})
	c.cache.SetTTL(time.Minute*5, false)
	c.cache.Start(time.Second * 10)
	return c
}

// GetByID attempts to fetch an emojiCategory from the cache by its ID, you will receive a copy for thread-safety
func (c *EmojiCategoryCache) GetByID(id string) (*gtsmodel.EmojiCategory, bool) {
	return c.cache.Get(id)
}

// GetByName attempts to fetch an emojiCategory from the cache by its name, you will receive a copy for thread-safety
func (c *EmojiCategoryCache) GetByName(name string) (*gtsmodel.EmojiCategory, bool) {
	return c.cache.GetBy("name", strings.ToLower(name))
}

// Put places an emojiCategory in the cache, ensuring that the object place is a copy for thread-safety
func (c *EmojiCategoryCache) Put(emoji *gtsmodel.EmojiCategory) {
	if emoji == nil || emoji.ID == "" {
		panic("invalid emoji")
	}
	c.cache.Set(emoji.ID, copyEmojiCategory(emoji))
}

func (c *EmojiCategoryCache) Invalidate(emojiID string) {
	c.cache.Invalidate(emojiID)
}

func copyEmojiCategory(emojiCategory *gtsmodel.EmojiCategory) *gtsmodel.EmojiCategory {
	return &gtsmodel.EmojiCategory{
		ID:        emojiCategory.ID,
		CreatedAt: emojiCategory.CreatedAt,
		UpdatedAt: emojiCategory.UpdatedAt,
		Name:      emojiCategory.Name,
	}
}