mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-12 23:01:40 +00:00
Cache all the things
This commit is contained in:
parent
ad07b169b8
commit
3d31b131fc
@ -68,12 +68,25 @@ func (d *Database) eventTypeNIDs(
|
|||||||
ctx context.Context, txn *sql.Tx, eventTypes []string,
|
ctx context.Context, txn *sql.Tx, eventTypes []string,
|
||||||
) (map[string]types.EventTypeNID, error) {
|
) (map[string]types.EventTypeNID, error) {
|
||||||
result := make(map[string]types.EventTypeNID)
|
result := make(map[string]types.EventTypeNID)
|
||||||
nids, err := d.EventTypesTable.BulkSelectEventTypeNID(ctx, txn, eventTypes)
|
// first try the cache
|
||||||
if err != nil {
|
fetchEventTypes := make([]string, 0, len(eventTypes))
|
||||||
return nil, err
|
for _, eventType := range eventTypes {
|
||||||
|
eventTypeNID, ok := d.Cache.GetEventTypeKey(eventType)
|
||||||
|
if ok {
|
||||||
|
result[eventType] = eventTypeNID
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fetchEventTypes = append(fetchEventTypes, eventType)
|
||||||
}
|
}
|
||||||
for eventType, nid := range nids {
|
if len(fetchEventTypes) > 0 {
|
||||||
result[eventType] = nid
|
nids, err := d.EventTypesTable.BulkSelectEventTypeNID(ctx, txn, fetchEventTypes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for eventType, nid := range nids {
|
||||||
|
result[eventType] = nid
|
||||||
|
d.Cache.StoreEventTypeKey(nid, eventType)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
@ -90,13 +103,15 @@ func (d *Database) EventStateKeys(
|
|||||||
fetch = append(fetch, nid)
|
fetch = append(fetch, nid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fromDB, err := d.EventStateKeysTable.BulkSelectEventStateKey(ctx, nil, fetch)
|
if len(fetch) > 0 {
|
||||||
if err != nil {
|
fromDB, err := d.EventStateKeysTable.BulkSelectEventStateKey(ctx, nil, fetch)
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
for nid, key := range fromDB {
|
}
|
||||||
result[nid] = key
|
for nid, key := range fromDB {
|
||||||
d.Cache.StoreEventStateKey(nid, key)
|
result[nid] = key
|
||||||
|
d.Cache.StoreEventStateKey(nid, key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
@ -130,6 +145,7 @@ func (d *Database) eventStateKeyNIDs(
|
|||||||
}
|
}
|
||||||
for eventStateKey, nid := range nids {
|
for eventStateKey, nid := range nids {
|
||||||
result[eventStateKey] = nid
|
result[eventStateKey] = nid
|
||||||
|
d.Cache.StoreEventStateKey(nid, eventStateKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user