~evanj/cms

ref: ce1376fafcd1233f8f280d81595846a3ec1684cb cms/internal/s/cache/contenttype.go -rw-r--r-- 4.0 KiB
ce1376faEvan M Jones Merge branch 'master' of git.sr.ht:~evanj/cms 1 year, 1 month 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
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("contenttype::%s::%s::%s", c.baseKey, space.ID(), thing.ID()),
		func() (contenttype.ContentType, error) { return thing, err },
	)
}

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

	iter, t, err := c.db.ContentIter(space, next, "name") // TODO: What is this sort type for?
	if err != nil {
		return nil, errors.Wrap(err, "failed to iterate content for cache breaking")
	}
	defer t.Rollback()

	// Remove content from cache that referenced this.
	for iter.Next() {
		prevC, err := iter.Scan()
		if err != nil {
			return nil, err
		}

		err = c.mc.Delete(fmt.Sprintf("contenttype::%s::%s::%s::%s", c.baseKey, space.ID(), next.ID(), prevC.ID()))
		if err != nil && !errors.Is(err, memcache.ErrCacheMiss) { // Don't care about cache miss.
			return nil, errors.Wrap(err, "failed to cache break content")
		}
	}

	if err := t.Commit(); err != nil {
		return nil, err
	}

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

func (c *Cache) ContentTypeGet(space space.Space, thingID string) (contenttype.ContentType, error) {
	return c.contenttype(
		false,
		fmt.Sprintf("contenttype::%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("contenttype::%s::%s::%s", c.baseKey, space.ID(), ct.ID())

	// Copy all contents and their values.
	iter, t, 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")
	}
	defer t.Rollback()

	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...)
	}

	if err := t.Commit(); err != nil {
		return err
	}

	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("contenttype::%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 errors.Wrap(err, "failed to cache break referring content")
		}
	}

	return c.mc.Delete(key)
}

// TODO: ContentTypeUpdate
// TODO: ContentTypesPerSpace