diff options
author | Alex Auvolat <alex@adnab.me> | 2020-02-21 18:43:47 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-02-21 18:43:47 +0100 |
commit | e1b838d30493effbcd8a23fe43e2b131c745b722 (patch) | |
tree | 6f5cab8d0e91c8c248cb2e79c1d116de99273c83 /appservice/db.go | |
parent | fd768a10be36ec31f674fa291fcbe77b78a2855c (diff) | |
download | easybridge-e1b838d30493effbcd8a23fe43e2b131c745b722.tar.gz easybridge-e1b838d30493effbcd8a23fe43e2b131c745b722.zip |
Implement on-demand updating of room & user pictures
Diffstat (limited to 'appservice/db.go')
-rw-r--r-- | appservice/db.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/appservice/db.go b/appservice/db.go index 5cefd30..646dae3 100644 --- a/appservice/db.go +++ b/appservice/db.go @@ -23,6 +23,8 @@ func InitDb() error { return err } + db.AutoMigrate(&DbCache{}) + db.AutoMigrate(&DbUserMap{}) db.Model(&DbUserMap{}).AddIndex("idx_protocol_user", "protocol", "user_id") @@ -35,6 +37,14 @@ func InitDb() error { return nil } +// Long-term cache entries +type DbCache struct { + gorm.Model + + Key string `gorm:"unique_index"` + Value string +} + // User mapping between protocol user IDs and puppeted matrix ids type DbUserMap struct { gorm.Model @@ -76,6 +86,30 @@ type DbPmRoomMap struct { // ---- +func dbCacheGet(key string) string { + var entry DbCache + if db.Where(&DbCache{Key: key}).First(&entry).RecordNotFound() { + return "" + } else { + return entry.Value + } +} + +func dbCachePut(key string, value string) { + var entry DbCache + db.Where(&DbCache{Key: key}).Assign(&DbCache{Value: value}).FirstOrCreate(&entry) +} + +func dbCacheTestAndSet(key string, value string) bool { + // TODO make this really an atomic operation + // True if value was changed, false if was already set + if dbCacheGet(key) != value { + dbCachePut(key, value) + return true + } + return false +} + func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) { var room DbRoomMap |