-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] simple cache & persistent storage by sqlite
- Loading branch information
Showing
8 changed files
with
259 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package storage | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
_ "modernc.org/sqlite" | ||
) | ||
|
||
type ( | ||
DatabaseType int | ||
InitDatabaseOption struct { | ||
SqlitePath string | ||
DatabaseType DatabaseType | ||
} | ||
) | ||
|
||
var ( | ||
db *sql.DB | ||
defaultInitDatabaseOption = &InitDatabaseOption{ | ||
SqlitePath: "/tmp/exif-web.sqlite3", | ||
DatabaseType: SQLite, | ||
} | ||
) | ||
|
||
const ( | ||
InMemorySQLite DatabaseType = iota | ||
SQLite | ||
) | ||
|
||
func init() { | ||
InitDatabase(nil) | ||
} | ||
|
||
func InitDatabase(option *InitDatabaseOption) { | ||
if option == nil { | ||
option = defaultInitDatabaseOption | ||
} | ||
var database *sql.DB | ||
var err error | ||
switch option.DatabaseType { | ||
case InMemorySQLite: | ||
database, err = sql.Open("sqlite", ":memory:") | ||
case SQLite: | ||
database, err = sql.Open("sqlite", option.SqlitePath) | ||
} | ||
if err != nil || database == nil { | ||
panic("failed to open database") | ||
} else { | ||
fmt.Println("open database success") | ||
db = database | ||
} | ||
initSchema() | ||
} | ||
|
||
func initSchema() { | ||
_, err := db.Exec(` | ||
CREATE TABLE IF NOT EXISTS kv ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
key VARCHAR(512) NOT NULL UNIQUE, | ||
value TEXT NOT NULL, | ||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
`) | ||
if err != nil { | ||
panic("failed to init schema") | ||
} | ||
} | ||
|
||
func Store(key, value string) (err error) { | ||
_, err = db.Exec(`INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)`, key, value) | ||
return | ||
} | ||
|
||
func Get(key string) (value string, err error) { | ||
err = db.QueryRow(`SELECT value FROM kv WHERE key = ? LIMIT 1`, key).Scan(&value) | ||
return | ||
} | ||
|
||
func DB() *sql.DB { | ||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package storage | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
_ "modernc.org/sqlite" | ||
"testing" | ||
) | ||
|
||
func TestInitDatabase_InMemorySQLite(t *testing.T) { | ||
InitDatabase(&InitDatabaseOption{ | ||
DatabaseType: InMemorySQLite, | ||
}) | ||
db := DB() | ||
err := db.Ping() | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestOpenDatabase_SQLite(t *testing.T) { | ||
InitDatabase(&InitDatabaseOption{ | ||
DatabaseType: SQLite, | ||
SqlitePath: "/tmp/exif-web.sqlite", | ||
}) | ||
db := DB() | ||
err := db.Ping() | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_Store(t *testing.T) { | ||
InitDatabase(&InitDatabaseOption{ | ||
DatabaseType: SQLite, | ||
SqlitePath: "/tmp/exif-web.sqlite3", | ||
}) | ||
var err error | ||
err = Store("key", "value") | ||
assert.Nil(t, err) | ||
} | ||
|
||
func Test_Get(t *testing.T) { | ||
InitDatabase(&InitDatabaseOption{ | ||
DatabaseType: InMemorySQLite, | ||
}) | ||
|
||
err := Store("key", "value") | ||
assert.Nil(t, err) | ||
value, err := Get("key") | ||
assert.True(t, value == "value") | ||
|
||
err = Store("key", "value-1") | ||
assert.Nil(t, err) | ||
value, err = Get("key") | ||
assert.True(t, value == "value-1") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.