GithubHelp home page GithubHelp logo

hiqsociety / cachehash Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zmap/cachehash

0.0 1.0 0.0 54 KB

An efficient C hash-table like data structure with static size that evicts LRU object on insertion

License: Apache License 2.0

Makefile 1.95% C 98.05%

cachehash's Introduction

CacheHash

An efficient C hash table data structure that supports a limited number of hash table items and evicts the least recently used object (with a callback) on insertion. This is accomplished with a Judy Array where hash table items point to an statically allocated doubly-linked-list where head is MRU object and tail is LRU. On object access, object is moved to the head of the list.

This is primarily intended for caches.

API

Initialization:

typedef void (*cachehash_evict_cb)(void* key, size_t keylen, void *data)
*cachehash cachehash_init(size_t maxitems, cachehash_evict_cb *cb)

Has Item? (returns item without rearranging):

void* cachehash_has(cachehash *ch, void *key, size_t key_len)

Get Item! (returns item and moves to be MRU object):

void* cachehash_get(cachehash *ch, void *key, size_t key_len)

Put Item! (adds an item to the cachehash):

void* cachehash_pet(cachehash *ch, void *key, size_t key_len, void *value)

Evict Item! (evicts LRU item if the cachehash is full):

void* cachehash_evict_if_full(cachehash *ch)

Free Cache Hash (frees all memory used by data structure):

void cachehash_free(cachehash *ch, cachehash_process_cb *cb);

Iterate (call callback for each item in hash table from MRU -> LRU order):

void cachehash_iter(cachehash *ch, cachehash_process_cb *cb);

Debug Print (print out entire data structure assuming that keys and values are null-terminated strings):

void cachehash_debug_dump(cachehash *ch);

Example

A quick example:

include <cachehash.h>
ch = cachehash_init(5, NULL);

char *key = "test-key";
char *value = "test-value";
cachehash_put(ch, key, strlen(key), value);

printf("value of has is %s\n", cachehash_has(ch, key, strlen(key)));
assert(cachehash_has(ch, key, strlen(key)) == value);

cachehash_free(ch, NULL);

Dependencies

CacheHash uses Judy Arrays (http://judy.sourceforge.net/) for its backend hash table. This can generally be installed using your operating system's package manager. For example, you need to run sudo apt-get install libjudy-dev on Debian-based systems.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See LICENSE for the specific language governing permissions and limitations under the License.

cachehash's People

Contributors

zakird avatar dadrian avatar hiqsociety avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.