visibility.go (2666B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cache 19 20 import ( 21 "codeberg.org/gruf/go-cache/v3/result" 22 "github.com/superseriousbusiness/gotosocial/internal/config" 23 ) 24 25 type VisibilityCache struct { 26 *result.Cache[*CachedVisibility] 27 } 28 29 // Init will initialize the visibility cache in this collection. 30 // NOTE: the cache MUST NOT be in use anywhere, this is not thread-safe. 31 func (c *VisibilityCache) Init() { 32 c.Cache = result.New([]result.Lookup{ 33 {Name: "ItemID", Multi: true}, 34 {Name: "RequesterID", Multi: true}, 35 {Name: "Type.RequesterID.ItemID"}, 36 }, func(v1 *CachedVisibility) *CachedVisibility { 37 v2 := new(CachedVisibility) 38 *v2 = *v1 39 return v2 40 }, config.GetCacheVisibilityMaxSize()) 41 c.Cache.SetTTL(config.GetCacheVisibilityTTL(), true) 42 c.Cache.IgnoreErrors(ignoreErrors) 43 } 44 45 // Start will attempt to start the visibility cache, or panic. 46 func (c *VisibilityCache) Start() { 47 tryStart(c.Cache, config.GetCacheVisibilitySweepFreq()) 48 } 49 50 // Stop will attempt to stop the visibility cache, or panic. 51 func (c *VisibilityCache) Stop() { 52 tryStop(c.Cache, config.GetCacheVisibilitySweepFreq()) 53 } 54 55 // VisibilityType represents a visibility lookup type. 56 // We use a byte type here to improve performance in the 57 // result cache when generating the key. 58 type VisibilityType byte 59 60 const ( 61 // Possible cache visibility lookup types. 62 VisibilityTypeAccount = VisibilityType('a') 63 VisibilityTypeStatus = VisibilityType('s') 64 VisibilityTypeHome = VisibilityType('h') 65 VisibilityTypePublic = VisibilityType('p') 66 ) 67 68 // CachedVisibility represents a cached visibility lookup value. 69 type CachedVisibility struct { 70 // ItemID is the ID of the item in question (status / account). 71 ItemID string 72 73 // RequesterID is the ID of the requesting account for this visibility lookup. 74 RequesterID string 75 76 // Type is the visibility lookup type. 77 Type VisibilityType 78 79 // Value is the actual visibility value. 80 Value bool 81 }