Compare commits
2 commits
d09b763aa7
...
1e0bc86b21
Author | SHA1 | Date | |
---|---|---|---|
1e0bc86b21 | |||
8fa74fd046 |
2 changed files with 40 additions and 3 deletions
12
internal/cache/cache_test.go
vendored
12
internal/cache/cache_test.go
vendored
|
@ -1,6 +1,8 @@
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -8,15 +10,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCache(t *testing.T) {
|
func TestCache(t *testing.T) {
|
||||||
// Create temporary database for testing
|
// Create temporary database for testing with unique name
|
||||||
database, err := db.New("test_cache.db")
|
dbFile := fmt.Sprintf("test_cache_%d.db", time.Now().UnixNano())
|
||||||
|
database, err := db.New(dbFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create test database: %v", err)
|
t.Fatalf("Failed to create test database: %v", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = database.Close()
|
_ = database.Close()
|
||||||
// Clean up test database file
|
// Clean up test database file
|
||||||
// os.Remove("test_cache.db")
|
_ = os.Remove(dbFile)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Create cache instance
|
// Create cache instance
|
||||||
|
@ -76,6 +79,9 @@ func TestCache(t *testing.T) {
|
||||||
t.Run("Exists", func(t *testing.T) {
|
t.Run("Exists", func(t *testing.T) {
|
||||||
existsKey := "exists_key"
|
existsKey := "exists_key"
|
||||||
|
|
||||||
|
// Make sure the key doesn't exist initially by deleting it
|
||||||
|
_ = cache.Delete(existsKey)
|
||||||
|
|
||||||
// Should not exist initially
|
// Should not exist initially
|
||||||
exists, err := cache.Exists(existsKey)
|
exists, err := cache.Exists(existsKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -35,6 +35,11 @@ func New(dbPath string) (*Database, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Configure SQLite for better reliability
|
||||||
|
if err := configureSQLite(db); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize database
|
// Initialize database
|
||||||
if err := initDatabase(db); err != nil {
|
if err := initDatabase(db); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -794,6 +799,32 @@ func initDatabase(db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Configure SQLite for better reliability
|
||||||
|
func configureSQLite(db *sql.DB) error {
|
||||||
|
pragmas := []string{
|
||||||
|
// Enable Write-Ahead Logging for better concurrency and crash recovery
|
||||||
|
"PRAGMA journal_mode = WAL",
|
||||||
|
// Set 5-second timeout when database is locked by another connection
|
||||||
|
"PRAGMA busy_timeout = 5000",
|
||||||
|
// Balance between safety and performance for disk writes
|
||||||
|
"PRAGMA synchronous = NORMAL",
|
||||||
|
// Set large cache size (1GB) for better read performance
|
||||||
|
"PRAGMA cache_size = 1000000000",
|
||||||
|
// Enable foreign key constraint enforcement
|
||||||
|
"PRAGMA foreign_keys = true",
|
||||||
|
// Store temporary tables and indices in memory for speed
|
||||||
|
"PRAGMA temp_store = memory",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pragma := range pragmas {
|
||||||
|
if _, err := db.Exec(pragma); err != nil {
|
||||||
|
return fmt.Errorf("failed to execute %s: %w", pragma, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// CacheGet retrieves a value from the cache
|
// CacheGet retrieves a value from the cache
|
||||||
func (d *Database) CacheGet(key string) (string, error) {
|
func (d *Database) CacheGet(key string) (string, error) {
|
||||||
query := `
|
query := `
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue