added LRU caching and communities data

This commit is contained in:
2025-09-22 23:22:46 -06:00
parent ef99606fb1
commit 7e11d0273a
5 changed files with 135 additions and 15 deletions
+77
View File
@@ -0,0 +1,77 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// The database package contains database management and storage logic.
package database
import (
"fmt"
"time"
lru "github.com/hashicorp/golang-lru"
)
// Community struct contains the high level data for a community.
type Community struct {
Id int32 `db:"commid"`
CreateDate time.Time `db:"createdate"`
LastAccess *time.Time `db:"lastaccess"`
LastUpdate *time.Time `db:"lastupdate"`
ReadLevel uint16 `db:"read_lvl"`
WriteLevel uint16 `db:"write_lvl"`
CreateLevel uint16 `db:"create_lvl"`
JoinLevel uint16 `db:"join_lvl"`
ContactId int32 `db:"contactid"`
HostUid *int32 `db:"host_uid"`
CategoryId int32 `db:"catid"`
HideFromDirectory bool `db:"hide_dir"`
HideFromSearch bool `db:"hide_search"`
MembersOnly bool `db:"membersonly"`
IsAdmin bool `db:"is_admin"`
InitFeature int16 `db:"init_ftr"`
Name string `db:"commname"`
Language *string `db:"language"`
Synopsis *string `db:"synopsis"`
Rules *string `dd:"rules"`
JoinKey *string `db:"joinkey"`
Alias string `db:"alias"`
}
// communityCache is the cache for Community objects.
var communityCache *lru.TwoQueueCache = nil
/* AmGetCommunity returns a reference to the specified community.
* Parameters:
* id - The ID of the community.
* Returns:
* Pointer to Community containing community data, or nil
* Standard Go error status
*/
func AmGetCommunity(id int32) (*Community, error) {
var err error = nil
if communityCache == nil {
communityCache, err = lru.New2Q(50)
if err != nil {
return nil, err
}
}
rc, ok := communityCache.Get(id)
if !ok {
var dbdata []Community
err = amdb.Select(&dbdata, "SELECT * from communities WHERE commid = ?", id)
if err != nil {
return nil, err
}
if len(dbdata) > 1 {
return nil, fmt.Errorf("AmGetCommunity(%d): too many responses(%d)", id, len(dbdata))
}
rc = &(dbdata[0])
communityCache.Add(id, rc)
}
return rc.(*Community), err
}
+47 -14
View File
@@ -10,8 +10,11 @@
package database
import (
"database/sql"
"fmt"
"time"
lru "github.com/hashicorp/golang-lru"
)
// User represents a user in the Amsterdam database.
@@ -34,6 +37,12 @@ type User struct {
DOB *time.Time `db:"dob"`
}
// userCache is the cache for User objects.
var userCache *lru.TwoQueueCache = nil
// anonUid is the UID of the "anonymous" user.
var anonUid int32 = -1
/* AmGetUser returns a reference to the specified user.
* Parameters:
* uid - The UID of the user.
@@ -42,15 +51,27 @@ type User struct {
* Standard Go error status
*/
func AmGetUser(uid int32) (*User, error) {
var rc []User
err := amdb.Select(&rc, "SELECT * from users WHERE uid = ?", uid)
if err != nil {
return nil, err
var err error = nil
if userCache == nil {
userCache, err = lru.New2Q(100)
if err != nil {
return nil, err
}
}
if len(rc) > 1 {
return nil, fmt.Errorf("AmGetUser(%d): too many responses(%d)", uid, len(rc))
rc, ok := userCache.Get(uid)
if !ok {
var dbdata []User
err = amdb.Select(&dbdata, "SELECT * from users WHERE uid = ?", uid)
if err != nil {
return nil, err
}
if len(dbdata) > 1 {
return nil, fmt.Errorf("AmGetUser(%d): too many responses(%d)", uid, len(dbdata))
}
rc = &(dbdata[0])
userCache.Add(uid, rc)
}
return &(rc[0]), err
return rc.(*User), err
}
/* AmGetAnonUser returns a reference to the anonymous user.
@@ -59,13 +80,25 @@ func AmGetUser(uid int32) (*User, error) {
* Standard Go error status
*/
func AmGetAnonUser() (*User, error) {
var rc []User
err := amdb.Select(&rc, "SELECT * from users WHERE is_anon = 1")
if err != nil {
return nil, err
var err error = nil
if anonUid < 0 {
var rows *sql.Rows
rows, err = amdb.Query("SELECT uid FROM users WHERE is_anon = 1")
if err == nil {
defer rows.Close()
if rows.Next() {
err = rows.Scan(&anonUid)
if err == nil && rows.Next() {
err = fmt.Errorf("should be only one anonymous user in Amsterdam database")
}
} else {
err = fmt.Errorf("no anonymous user in Amsterdam database")
}
}
}
if len(rc) > 1 {
return nil, fmt.Errorf("AmGetAnonUser: too many responses(%d)", len(rc))
var rc *User = nil
if err == nil {
rc, err = AmGetUser(anonUid)
}
return &(rc[0]), err
return rc, err
}