~evanj/cms

ref: d8267364d7e704c2653e785c08fa0c081efc863b cms/internal/s/cache/contenttype.go -rw-r--r-- 3.2 KiB
d8267364Evan M Jones Fix(cache break): When a contenttype is deleted break cache for all 1 year, 1 day 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
package cache

import (
	"fmt"

	"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"
	"github.com/pkg/errors"
)

// TODO: ON DELETE CASCADE
// BIG TODO: ON DELETE CASCADE
// Because of this we need to loop through array keys for content when a
// contenttype is deleted and remove from cache.

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

func (c *Cache) ContentTypeNew(space space.Space, name string, params []db.ContentTypeNewParam) (contenttype.ContentType, error) {
	thing, err := c.db.ContentTypeNew(space, name, params)
	if err != nil {
		return nil, err
	}

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

func (c *Cache) ContentTypeUpdate(space space.Space, thing contenttype.ContentType, name string, newParams []db.ContentTypeNewParam, updateParams []db.ContentTypeUpdateParam) (contenttype.ContentType, error) {
	thing, err := c.db.ContentTypeUpdate(space, thing, name, newParams, updateParams)
	if err != nil {
		return nil, err
	}

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

func (c *Cache) ContentTypeGet(space space.Space, thingID string) (contenttype.ContentType, error) {
	return c.contenttype(
		false,
		fmt.Sprintf("%s::%s::%s", c.baseKey, space.ID(), thingID),
		func() (contenttype.ContentType, error) { return c.db.ContentTypeGet(space, thingID) },
	)
}

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

	// Copy all contents and their values.
	iter, err := c.db.ContentIter(space, ct, "name") // TODO: What is this sort type for?
	if err != nil {
		return errors.Wrap(err, "failed to iterate referring content for cache breaking")
	}

	var list []db.ContentRefSet
	for iter.Next() {
		prevC, err := iter.Scan()
		if err != nil {
			return err
		}

		subList, err := c.db.ContentRefererList(prevC.ID())
		if err != nil {
			return errors.Wrap(err, "failed to find referring content for cache breaking")
		}

		list = append(list, subList...)
	}

	var deleteErr error
	_, _ = c.contenttype(
		true,
		key,
		func() (contenttype.ContentType, error) {
			deleteErr = c.db.ContentTypeDelete(space, ct)
			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("%s::%s::%s::%s", c.baseKey, space.ID(), ref.ContentTypeID, ref.ContentID))
		if !errors.Is(err, memcache.ErrCacheMiss) { // Don't care about cache miss.
			return errors.Wrap(err, "failed to cache break referring content")
		}
	}

	return c.mc.Delete(key)
}

// TODO: ContentTypeUpdate
// TODO: ContentTypesPerSpace