⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccache.c

📁 cache库 别人写的
💻 C
字号:
/********************************************************************
	created:	2008/01/23
	filename: 	ccache.c
	author:		Lichuang
                
	purpose:    
*********************************************************************/

#include "ccache.h"
#include "lock.h"
#include "shm.h"
#include "hash.h"
#include "operator.h"
#include "lrulist.h"
#include "node.h"

cache_t* create_cache(int nodenum, int datasize, int hashitemnum, int keysize, const char* mapfilename, int attach)
{
    return create_shm(nodenum, datasize, hashitemnum, keysize, mapfilename, attach);
}

int destroy_cache(cache_t* cache)
{
    return destroy_shm(cache);
}

int insert_data(const void* key, const void* data, cache_t* cache, comfun cmp, delfun fun)
{
    int hashindex = hash(key, cache), ret;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = INSERT_NODE(hashindex, key, data, cache, cmp, fun);

    if (0 <= ret)
        linktolrulisthead(ret, cache);

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int find_data(const void* key, void* data, cache_t* cache, comfun cmp)
{
    int hashindex = hash(key, cache), ret;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = FIND_NODE(hashindex, key, data, cache, cmp);

    if (0 <= ret)
        linktolrulisthead(ret, cache);

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int update_data(const void* key, const void* data, cache_t* cache, comfun cmp)
{
    int hashindex = hash(key, cache), ret;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = UPDATE_NODE(hashindex, key, data, cache, cmp);

    if (0 <= ret)
        linktolrulisthead(ret, cache);

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int delete_data(const void* key, void* data, cache_t* cache, comfun cmp)
{
    int hashindex = hash(key, cache), ret;
    node_t *node;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = FIND_NODE(hashindex, key, data, cache, cmp);
    if (0 > ret)
    {
        unlock(&(cache->mutex));
        return -1;
    }
    ret = DELETE_NODE(hashindex, ret, cache, cmp);
    if (0 <= ret)
    {
        node = NODE(cache, ret);
        node->next = cache->firstfreenode;
        node->prev = -1;
        if (0 <= cache->firstfreenode)
        {
            node = NODE(cache, cache->firstfreenode);
            node->prev = ret;
        }
        cache->firstfreenode = ret;
    }

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -