zhangyuchun edited this page Nov 27, 2018 · 7 revisions

Cache

Support memory / redis / memcache / rocksdb

Config

application.conf

memory:

cache.storage = memory

redis:

cache.storage = redis
cache.args = 127.0.0.1:6379

cache

import hunt.cache;

struct Student
{
	ulong 		ID;
	string 		name;
	string		address;
}

class Grade
{
	int 		gradeLevel;
	string  	gradeName;
	Student[]	arrStu;
}

auto cache = UCache.CreateUCache();

//string
cache.put("test" , "test");
string val = cache.get("test");

//student.
Student stu1;
stu1.ID = 1;
stu1.name = "tom";
stu1.address = "Tianlin Road 1016";
cache.put("tom" , stu1);
auto stu = cache.get!Student("tom");

//Grade
Grade grade = new Grade();
grade.gradeLevel = 12;
grade.gradeName = "13";
grade.arrStu ~= stu1;
cache.put("13" , grade);
auto grade1 = cache.get!Grade("13");

Use cache in Controller

@Action
void index()
{
    cache.put("a", "AAA")

    response.html(cache.get("a"));
}

Use cache object in other code

import hunt.framework;

import std.stdio : writeln;

app.cache.put("a", "Hello world!");

writeln(app.cache.get("a"));
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.