Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add optional cache for tables #156

Open
serjek opened this issue Apr 19, 2024 · 0 comments
Open

Proposal: add optional cache for tables #156

serjek opened this issue Apr 19, 2024 · 0 comments

Comments

@serjek
Copy link

serjek commented Apr 19, 2024

I am thinking about something like this:

class Db extends tink.sql.Database {
  @:table @:cache var DataPoints:DataPoints;
}

where @:cache will generate an internal class like this:

class Memcached<T> implements coconut.data.Model {
	@:constant var loadData:Void->Promise<Array<T>>;
	@:constant var dbInsertOne:T->Promise<Noise> = @byDefault _ -> Promise.lift(new Error("not implemented"));
	@:constant var dbInsertMany:Array<T>->Promise<Noise> = @byDefault _ -> Promise.lift(new Error("not implemented"));

	@:loaded var loadedData:Vector<T> = loadData().next(Vector.fromArray);
	@:computed var data:Vector<T> = loadedData.or(Vector.empty()) & cachedData;
	@:computed var isLoaded:Bool = loadedData != Loading;
	@:observable var cachedData:Vector<T> = Vector.empty();

	@:transition function insertOne(v:T) {
		dbInsertOne(v).handle(v -> switch v {
			case Success(_):
			case Failure(e): throw e;
		});
		return {cachedData: cachedData & v}
	}
	@:transition function insertMany(v:Array<T>) {
		dbInsertMany(v).handle(v -> switch v {
			case Success(_):
			case Failure(e): throw e;
		});
		return {cachedData: cachedData & Vector.fromArray(v)}
	}
}

and declared like this

var memcachedDataPoints:Memcached<DataPoints> = new Memcached({
  loadData: db.DataPoints.select().all,
  dbInsertOne: db.DataPoints.insertOne,
  dbInsertMany: db.DataPoints.insertMany
});

And then data still can be accessed (set/get) directly through table declaration, with data flow processed by memcached middleware.

Pro: easy to use application level cache with unified and clean api
Con: extra dependency (coconut)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant