Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Latest commit

 

History

History
66 lines (49 loc) · 1.63 KB

README.md

File metadata and controls

66 lines (49 loc) · 1.63 KB

liquid-cache

Cache block for the liquid templating engine ( https://github.com/FerarDuanSednan/liquid-rust )

Still basic, PR are welcome, I'm not anymore improving this.

TODO

  • Improve caching keys ( md5 of the content, etc )
  • Allow a 2nd arg to the filter to set the expiration time

Exemples :

Cache into a folder

let vec = vec![Value::Str("GOOOO".to_string()); 100000];

let text = r#"
{%cache makey %}
  {% for i in vec %}
    My stuff {{ i }}
  {% endfor %}
{% endcache %}
"#;

let mut options : LiquidOptions = Default::default();
options.blocks.insert("cache".to_string(), Box::new(RawCacheBlock::new("./tests/tmp")));

let template = parse(&text, &mut options).unwrap();

let mut data = Context::new();
data.set_val("vec", Value::Array(vec));
data.set_val("makey", Value::Str("cachekeytest".to_string())); // tmp file : ./tests/tmp/cachekeytest

let _ = template.render(&mut data);

Use redis to cache data

// redis
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let con = Arc::new(Mutex::new(client.get_connection().unwrap()));
// End

let vec = vec![Value::Str("GOOOO".to_string()); 100000];

let text = r#"
{%cache makey %}
  {% for i in vec %}
    My stuff {{ i }}
  {% endfor %}
{% endcache %}
"#;

let mut options : LiquidOptions = Default::default();
options.blocks.insert("cache".to_string(), Box::new(RedisCacheBlock::new(con.clone())));

let template = parse(&text, &mut options).unwrap();

let mut data = Context::new();
data.set_val("vec", Value::Array(vec));
data.set_val("makey", Value::Str("cachekeytest".to_string()));

let _ = template.render(&mut data);