Git Product home page Git Product logo

c-lru-'s Introduction

基于双向链表和哈希表实现的LRU进程内缓存包,C语言实现。改编自https://github.com/Stand1210/c-LRU-

Features

  • 测试全面
  • LRU淘汰算法
  • 双向链表和哈希表的存储模型
  • 基于键值对,键和值的数据类型为字符串
  • 对全局存储结构的写操作上锁,并发安全

Usage

#include <stdio.h>
#include <string.h>

#include "./LRU/lru_cache.h"
#include "./LRU/lru_cache_impl.h"

int main(int argc, char **argv){
    void *LruCache;

    //创建缓存器
    if (0 == LRUCacheCreate(3, &LruCache))
        printf("缓存器创建成功,容量为3\n");

    //向缓存器中添加数据
    if (0 != LRUCacheSet(LruCache, "key1", "value1"))
        printf("put (key1, value1) failed!\n");

    if (0 != LRUCacheSet(LruCache, "key2", "value2"))
        printf("put (key2, value2) failed!\n");

    if (0 != LRUCacheSet(LruCache, "key3", "value3"))
        printf("put (key3, value3) failed!\n");

    if (0 != LRUCacheSet(LruCache, "key4", "value4"))
        printf("put (key4, value4) failed!\n");

    if (0 != LRUCacheSet(LruCache, "key5", "value5"))
        printf("put (key5, value5) failed!\n");

    //按照顺序输出缓存器当前的内容
    LRUCachePrint(LruCache);

    //获取缓存器内容
    if (NULL == LRUCacheGet(LruCache, "key1")){
        printf("key1 所对应的数据未被缓存\n");
    }
    else{
        char data[VALUE_SIZE];
        strncpy(data, LRUCacheGet(LruCache, "key1"), VALUE_SIZE);
        printf("key1所对应的数据为:%s", data);
    }

    char value4[VALUE_SIZE];
    strncpy(value4, LRUCacheGet(LruCache, "key4"), VALUE_SIZE);
    printf("key4所对应的数据:%s", value4);

    LRUCachePrint(LruCache);

    //销毁缓存器
    if (0 != LRUCacheDestory(LruCache))
        printf("destory error");
}

输出:

缓存器创建成功,容量为3

>>>>>>>>>>>>>>>
cache (key data):
(key5, value5)(key4, value4)(key3, value3)
<<<<<<<<<<<<<<<
key1 所对应的数据未被缓存
key4所对应的数据:value4
>>>>>>>>>>>>>>>
cache (key data):
(key4, value4)(key5, value5)(key3, value3)
<<<<<<<<<<<<<<<

Last but not Least

本程序中的键和值在存储时所占用的字节数是写死的,不具有动态性,这是本程序的一个缺陷(怪就该C语言的字符串处理太麻烦了)。在应对键值长度与设定的长度不一致的场景下会出现浪费内存或者数据截断的问题。所以根据具体的场景合理的设定键和值所占用的字节数至关重要。设置方法:修改包中的lru_cache_impl.h文件中的KEY_SIZEVALUE_SIZE两个宏即可,这两个宏初始对应的值分别是50,100.

c-lru-'s People

Contributors

andrewpqc avatar stand1210 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.