~evanj/cms

ref: 2171152d07759a25fa315df4321c09ead16f83e7 cms/internal/s/cache/content.go -rw-r--r-- 4.7 KiB
2171152dEvan M Jones WIP 4 months ago
                                                                                
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package cache

import (
	"encoding/json"
	"errors"
	"fmt"

	"git.sr.ht/~evanj/cms/internal/m/content"
	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/s/db"
	"github.com/bradfitz/gomemcache/memcache"
)

func (c *Cache) content(breakCache bool, key string, getter func() (content.Content, error)) (content.Content, error) {
	var v *db.Content
	if err := c.cache(breakCache, key, &v, func() (interface{}, error) { return getter() }); err != nil {
		return nil, err
	}
	return v, nil
}

func (c *Cache) ContentNew(space space.Space, ct contenttype.ContentType, params []db.ContentNewParam) (content.Content, error) {
	thing, err := c.db.ContentNew(space, ct, params)
	if err != nil {
		return nil, err
	}

	return c.content(
		true,
		fmt.Sprintf("content::%s::%s::%s::%s", c.baseKey, space.ID(), ct.ID(), thing.ID()),
		func() (content.Content, error) { return thing, err },
	)
}

func (c *Cache) ContentGet(space space.Space, ct contenttype.ContentType, contentID string) (content.Content, error) {
	return c.content(
		false,
		fmt.Sprintf("content::%s::%s::%s::%s", c.baseKey, space.ID(), ct.ID(), contentID),
		func() (content.Content, error) { return c.db.ContentGet(space, ct, contentID) },
	)
}

func (c *Cache) ContentUpdate(space space.Space, ct contenttype.ContentType, item content.Content, newParams []db.ContentNewParam, updateParams []db.ContentUpdateParam) (content.Content, error) {
	content, err := c.content(
		true,
		fmt.Sprintf("content::%s::%s::%s::%s", c.baseKey, space.ID(), ct.ID(), item.ID()),
		func() (content.Content, error) { return c.db.ContentUpdate(space, ct, item, newParams, updateParams) },
	)
	if err != nil {
		return nil, err
	}

	list, err := c.db.ContentRefererList(item.ID())
	if err != nil {
		return nil, err
	}

	// Remove content from cache that referenced this.
	for _, ref := range list {
		err := c.mc.Delete(fmt.Sprintf("content::%s::%s::%s::%s", c.baseKey, space.ID(), ref.ContentTypeID, ref.ContentID))
		if err != nil && !errors.Is(err, memcache.ErrCacheMiss) { // Don't care about cache miss.
			return nil, err
		}
	}

	return content, nil
}

func (c *Cache) ContentDelete(space space.Space, ct contenttype.ContentType, item content.Content) error {
	key := fmt.Sprintf("content::%s::%s::%s::%s", c.baseKey, space.ID(), ct.ID(), item.ID())

	list, err := c.db.ContentRefererList(item.ID())
	if err != nil {
		return err
	}

	var deleteErr error
	_, _ = c.content(
		true,
		key,
		func() (content.Content, error) {
			deleteErr = c.db.ContentDelete(space, ct, item)
			return nil, deleteErr
		},
	)

	if deleteErr != nil {
		return deleteErr
	}

	// Remove content from cache that referenced this.
	for _, ref := range list {
		err := c.mc.Delete(fmt.Sprintf("content::%s::%s::%s::%s", c.baseKey, space.ID(), ref.ContentTypeID, ref.ContentID))
		if err != nil && !errors.Is(err, memcache.ErrCacheMiss) { // Don't care about cache miss.
			return err
		}
	}

	return c.mc.Delete(key)
}

func (c *Cache) ContentPerContentType(space space.Space, ct contenttype.ContentType, before int, order db.OrderType, sortField string) (content.ContentList, error) {
	// Check if in cache.
	// Put into cache.
	// Put individual items in cache, pointing to list cache for invalidation.

	var (
		cl     db.ContentList
		key    = fmt.Sprintf("content::list::%s::%s::%s::%d::%s::%s", c.baseKey, space.ID(), ct.ID(), before, order.Val(), sortField)
		getter = func() (interface{}, error) {
			list, err := c.db.ContentPerContentType(space, ct, before, order, sortField)

			for _, content := range list.List() {
				cKey := fmt.Sprintf("content::list::item::%s::%s::%s", c.baseKey, space.ID(), content.ID())
				if err := c.updateContentCacheList(key, cKey, content); err != nil {
					return nil, err
				}
			}

			return list, err
		}
	)

	// TODO: Change to true.
	err := c.cache(true, key, &cl, getter)
	return &cl, err
}

func (c *Cache) updateContentCacheList(key, cKey string, content content.Content) error {
	// TODO: Loop through referenced items.

	// for _, v := range content.Values() {
	// 	switch v.Type() {
	// 	case valuetype.Reference:
	// 		break
	// 	case valuetype.ReferenceList:
	// 		break
	// 	}
	// }

	var listref struct{ Keys []string }
	it, err := c.mc.Get(cKey)
	if err == nil {
		// Item already in cache, hydrate value.
		c.log.Println("cache value being hydrated")

		if err := json.Unmarshal(it.Value, &listref); err != nil {
			c.log.Println("cache value corrupted")
			return err
		}
	}

	// Create/update value in cache.
	listref.Keys = append(listref.Keys, key)

	bytes, err := json.Marshal(listref)
	if err != nil {
		c.log.Println("cache value failed to marshal")
		return err
	}

	c.log.Println("cache value set")
	return c.mc.Set(&memcache.Item{Key: cKey, Value: bytes})
}

// TODO: ContentPerContentType