~evanj/cms

ref: 81dac613a00686c518230f2688a0893a658b61e3 cms/internal/s/cache/content.go -rw-r--r-- 3.0 KiB
81dac613Evan M Jones Feat(c/content): Testing complete for controllers. 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
package cache

import (
	"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)
}

// TODO: ContentPerContentType