grumpygamer / kvstore

Simple key/value store using PHP and SQLite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KVStore

Simple key/value store using PHP and SQLite.

This is intended for a low to medium traffic website. If you need to use it on a high traffic website, switching the MySQL or PostgreSQL would be preferred to SQLite.

KVStore supports the following operations:

get
put
inc
dec
add

KVStore fails silently in most situation unless the json param is passed.

Creating the database

When creating the database make sure it is in a location where the web server has read/write access.

sqlite3 some/path/kv.db

CREATE TABLE kv_store (
  id INTEGER PRIMARY KEY,
  key TEXT,
  value TEXT,
  project TEXT,
  time TEXT
);
CREATE TABLE auth (
  id INTEGER PRIMARY KEY,
  project TEXT,
  secret TEXT,
  name TEXT,
  email TEXT,
  disabled INTEGER
);
CREATE INDEX idx_key ON kv_store(key ASC);
INSERT INTO auth (project,secret) VALUES ("my_project", "qskpchkqupdqydkz");
PRAGMA journal_mode = WAL;
PRAGMA synchronous = 1;
.quit

Installing

Most modern installations of PHP already have the SQLite extention installed. If you're running an older version of PHP you will need to install the SQLite extention.

Place the kvstore.php file where the web server has access.

Copy kvstore_inc.sample.php to kvstore_inc.php and change $DB_NAME = "some/path/kv.db" to the location of the database file.

Testing

Once installed the key/value store can be tested with the following:

KVSTORE="https://example.com/kvstore.php?project=my_project&secret=qskpchkqupdqydkz"
curl -s -X POST -d "action=put" -d "key=games/favorite" -d "value=thimbleweedpark" $KVSTORE
curl -s -X POST -d "action=put" -d "key=test" -d "value=1" $KVSTORE
curl -s -X POST -d "action=inc" -d "key=test" $KVSTORE
curl -s -X POST -d "action=get" -d "key=games/favorite" $KVSTORE
curl -s -X POST -d "action=get" -d "key=games/favorite" -d "json=1" $KVSTORE
curl -s -X POST -d "action=inc" -d "key=test" -d "json=1" $KVSTORE

To check the key/value database:

sqlite3 some/path/kv.db

SELECT * FROM auth
SELECT * FROM kv_store
.quit

About

Simple key/value store using PHP and SQLite

License:MIT License


Languages

Language:PHP 100.0%