summaryrefslogtreecommitdiff
path: root/src/cache/cache.c
blob: 1169350f66b9b56b28c4d791303732bcf28c80aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* ISC license. */

#include <string.h>

#include <skalibs/uint16.h>
#include <skalibs/posixplz.h>
#include <skalibs/strerr.h>

#include <s6-dns/s6dns-domain.h>

#include <shibari/dcache.h>
#include "shibari-cache-internal.h"

static dcache_t cache = DCACHE_ZERO ;

void cache_init (uint64_t cachesize)
{
  dcache_init(&cache, cachesize) ;
}

void cache_dump (void)
{
  if (g->dumpfile)
  {
    if (!dcache_save(&cache, g->dumpfile))
    {
      strerr_warnwu2sys("save cache contents to ", g->dumpfile) ;
      unlink_void(g->dumpfile) ;
    }
  }
}

void cache_load (void)
{
  if (g->dumpfile)
  {
    if (!dcache_load(&cache, g->dumpfile))
      strerr_warnwu2sys("load cache contents from ", g->dumpfile) ;
  }
}

int cache_search (s6dns_domain_t const *name, uint16_t qtype, dcache_key_t *data)
{
  dcache_node_t *node ;
  char key[name->len + 1] ;
  uint16_pack_big(key, qtype) ;
  memcpy(key + 2, name->s, name->len - 1) ;
  node = dcache_search_g(&cache, key, name->len + 1) ;
  if (!node) return 0 ;
  data->s = node->key.s + node->key.len ;
  data->len = node->datalen ;
  return 1 ;
}