Rename DbCache to DbKv

This commit is contained in:
Alex 2020-02-28 10:34:22 +01:00
parent 30a5cdc2a3
commit 8668b12a81
10 changed files with 32 additions and 26 deletions

View file

@ -291,7 +291,7 @@ func (a *Account) userInfoUpdatedInternal(user UserID, info *UserInfo) error {
if info.Avatar != nil { if info.Avatar != nil {
cache_key := fmt.Sprintf("%s/user_avatar/%s", a.Protocol, user) cache_key := fmt.Sprintf("%s/user_avatar/%s", a.Protocol, user)
cache_val := info.Avatar.Filename() cache_val := info.Avatar.Filename()
if cache_val == "" || dbCacheTestAndSet(cache_key, cache_val) { if cache_val == "" || dbKvTestAndSet(cache_key, cache_val) {
err2 := mx.ProfileAvatar(mx_user_id, info.Avatar) err2 := mx.ProfileAvatar(mx_user_id, info.Avatar)
if err2 != nil { if err2 != nil {
err = err2 err = err2
@ -345,7 +345,7 @@ func (a *Account) roomInfoUpdatedInternal(roomId RoomID, author UserID, info *Ro
if info.Picture != nil { if info.Picture != nil {
cache_key := fmt.Sprintf("%s/room_picture/%s", a.Protocol, roomId) cache_key := fmt.Sprintf("%s/room_picture/%s", a.Protocol, roomId)
cache_val := info.Picture.Filename() cache_val := info.Picture.Filename()
if cache_val == "" || dbCacheTestAndSet(cache_key, cache_val) { if cache_val == "" || dbKvTestAndSet(cache_key, cache_val) {
err2 := mx.RoomAvatarAs(mx_room_id, info.Picture, as_mxid) err2 := mx.RoomAvatarAs(mx_room_id, info.Picture, as_mxid)
if err2 != nil { if err2 != nil {
err = err2 err = err2
@ -417,7 +417,7 @@ func (a *Account) eventInternal(event *Event) error {
if event.Id != "" { if event.Id != "" {
cache_key := fmt.Sprintf("%s/event_seen/%s/%s", cache_key := fmt.Sprintf("%s/event_seen/%s/%s",
a.Protocol, mx_room_id, event.Id) a.Protocol, mx_room_id, event.Id)
if !dbCacheTestAndSet(cache_key, "yes") { if !dbKvTestAndSet(cache_key, "yes") {
// false: cache key was not modified, meaning we // false: cache key was not modified, meaning we
// already saw the event // already saw the event
return nil return nil
@ -475,11 +475,11 @@ func (a *Account) eventInternal(event *Event) error {
func (a *Account) CacheGet(key string) string { func (a *Account) CacheGet(key string) string {
cache_key := fmt.Sprintf("%s/account/%s/%s/%s", cache_key := fmt.Sprintf("%s/account/%s/%s/%s",
a.Protocol, a.MatrixUser, a.AccountName, key) a.Protocol, a.MatrixUser, a.AccountName, key)
return dbCacheGet(cache_key) return dbKvGet(cache_key)
} }
func (a *Account) CachePut(key string, value string) { func (a *Account) CachePut(key string, value string) {
cache_key := fmt.Sprintf("%s/account/%s/%s/%s", cache_key := fmt.Sprintf("%s/account/%s/%s/%s",
a.Protocol, a.MatrixUser, a.AccountName, key) a.Protocol, a.MatrixUser, a.AccountName, key)
dbCachePut(cache_key, value) dbKvPut(cache_key, value)
} }

View file

@ -4,8 +4,10 @@ import (
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
) )
const IRC_PROTOCOL = "IRC"
func init() { func init() {
Register("irc", Protocol{ Register(IRC_PROTOCOL, Protocol{
NewConnector: func() Connector { return &IRC{} }, NewConnector: func() Connector { return &IRC{} },
Schema: ConfigSchema{ Schema: ConfigSchema{
&ConfigEntry{ &ConfigEntry{

View file

@ -32,7 +32,7 @@ func (irc *IRC) SetHandler(h Handler) {
} }
func (irc *IRC) Protocol() string { func (irc *IRC) Protocol() string {
return "irc" return IRC_PROTOCOL
} }
func (irc *IRC) Configure(c Configuration) error { func (irc *IRC) Configure(c Configuration) error {

View file

@ -4,8 +4,10 @@ import (
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
) )
const MATTERMOST_PROTOCOL = "Mattermost"
func init() { func init() {
Register("mattermost", Protocol{ Register(MATTERMOST_PROTOCOL, Protocol{
NewConnector: func() Connector { return &Mattermost{} }, NewConnector: func() Connector { return &Mattermost{} },
Schema: ConfigSchema{ Schema: ConfigSchema{
&ConfigEntry{ &ConfigEntry{

View file

@ -49,7 +49,7 @@ func (mm *Mattermost) SetHandler(h Handler) {
} }
func (mm *Mattermost) Protocol() string { func (mm *Mattermost) Protocol() string {
return "mattermost" return MATTERMOST_PROTOCOL
} }
func (mm *Mattermost) Configure(c Configuration) error { func (mm *Mattermost) Configure(c Configuration) error {

View file

@ -4,8 +4,10 @@ import (
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
) )
const XMPP_PROTOCOL = "XMPP"
func init() { func init() {
Register("xmpp", Protocol{ Register(XMPP_PROTOCOL, Protocol{
NewConnector: func() Connector { return &XMPP{} }, NewConnector: func() Connector { return &XMPP{} },
Schema: ConfigSchema{ Schema: ConfigSchema{
&ConfigEntry{ &ConfigEntry{

View file

@ -43,7 +43,7 @@ func (xm *XMPP) SetHandler(h Handler) {
} }
func (xm *XMPP) Protocol() string { func (xm *XMPP) Protocol() string {
return "xmpp" return XMPP_PROTOCOL
} }
func (xm *XMPP) Configure(c Configuration) error { func (xm *XMPP) Configure(c Configuration) error {

22
db.go
View file

@ -27,7 +27,7 @@ func InitDb() error {
db.AutoMigrate(&DbAccountConfig{}) db.AutoMigrate(&DbAccountConfig{})
db.AutoMigrate(&DbCache{}) db.AutoMigrate(&DbKv{})
db.AutoMigrate(&DbUserMap{}) db.AutoMigrate(&DbUserMap{})
db.Model(&DbUserMap{}).AddIndex("idx_protocol_user", "protocol", "user_id") db.Model(&DbUserMap{}).AddIndex("idx_protocol_user", "protocol", "user_id")
@ -55,7 +55,7 @@ type DbAccountConfig struct {
} }
// Long-term cache entries // Long-term cache entries
type DbCache struct { type DbKv struct {
gorm.Model gorm.Model
Key string `gorm:"unique_index"` Key string `gorm:"unique_index"`
@ -130,27 +130,27 @@ func dbUnlockSlot(key string) {
// ---- // ----
func dbCacheGet(key string) string { func dbKvGet(key string) string {
var entry DbCache var entry DbKv
if db.Where(&DbCache{Key: key}).First(&entry).RecordNotFound() { if db.Where(&DbKv{Key: key}).First(&entry).RecordNotFound() {
return "" return ""
} else { } else {
return entry.Value return entry.Value
} }
} }
func dbCachePut(key string, value string) { func dbKvPut(key string, value string) {
var entry DbCache var entry DbKv
db.Where(&DbCache{Key: key}).Assign(&DbCache{Value: value}).FirstOrCreate(&entry) db.Where(&DbKv{Key: key}).Assign(&DbKv{Value: value}).FirstOrCreate(&entry)
} }
func dbCacheTestAndSet(key string, value string) bool { func dbKvTestAndSet(key string, value string) bool {
dbLockSlot(key) dbLockSlot(key)
defer dbUnlockSlot(key) defer dbUnlockSlot(key)
// True if value was changed, false if was already set // True if value was changed, false if was already set
if dbCacheGet(key) != value { if dbKvGet(key) != value {
dbCachePut(key, value) dbKvPut(key, value)
return true return true
} }
return false return false

View file

@ -133,7 +133,7 @@ func handleTxnEvent(e *mxlib.Event) error {
} else if room := dbIsPublicRoom(e.RoomId); room != nil { } else if room := dbIsPublicRoom(e.RoomId); room != nil {
cache_key := fmt.Sprintf("%s/event_seen/%s/%s", cache_key := fmt.Sprintf("%s/event_seen/%s/%s",
room.Protocol, e.RoomId, ev.Id) room.Protocol, e.RoomId, ev.Id)
dbCachePut(cache_key, "yes") dbKvPut(cache_key, "yes")
// If this is a regular room // If this is a regular room
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID) acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
if acct != nil { if acct != nil {

View file

@ -34,8 +34,8 @@
<h5 class="mt-4">Add account</h5> <h5 class="mt-4">Add account</h5>
<a class="btn btn-sm btn-dark" href="/add/irc">IRC</a> <a class="btn btn-sm btn-dark" href="/add/IRC">IRC</a>
<a class="btn btn-sm btn-warning ml-4" href="/add/xmpp">XMPP</a> <a class="btn btn-sm btn-warning ml-4" href="/add/XMPP">XMPP</a>
<a class="btn btn-sm btn-info ml-4" href="/add/mattermost">Mattermost</a> <a class="btn btn-sm btn-info ml-4" href="/add/Mattermost">Mattermost</a>
{{end}} {{end}}