RejectedSoftware Forums

Sign up

How do I add an array in Session?


Re: How do I add an array in Session?

On Thu, 16 Nov 2017 14:24:27 GMT, John Smith wrote:


The message appears to have gotten lost, so I'll just summarize the intended status quo. Array types should be settable in sessions like normal values (both with Session.set and SessionVar!T).

However, they actually behave as value types, so adding or removing elements requires setting the whole array again (e.g. session.set("some_array", session.get!(int[])("some_array") ~ 1);). So if the array has a non-trivial size, it should be stored manually in a database or key/value store using the session ID as the lookup key.

Re: How do I add an array in Session?

OK. I trying with below example but I have got error.

import vibe.vibe;

void main()
	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	settings.bindAddresses = ["::1", ""];
	settings.sessionStore = new MemorySessionStore;

	listenHTTP(settings, &hello);

	logInfo("Please open in your browser.");

void hello(HTTPServerRequest req, HTTPServerResponse res)
	int[] arr = [1, 2, 3, 4];

	auto session = res.startSession();
	session.set("array", arr);

	res.writeBody("Hello, World!");
vibe-session ~master: building configuration "application"...
/home/astar/.dub/packages/vibe-d-0.8.2-rc.1/vibe-d/http/vibe/http/session.d(92,3): Error: static assert  "Type int[] contains references, which is not supported for session storage."
source/app.d(21,13):        instantiated from here: set!(int[])
dmd failed with exit code 1.