zoukankan      html  css  js  c++  java
  • New Caching Mechanism

    class e_Cache;

    class e_CacheSlot {

        friend 
    class e_Cache;

    public:
        e_CacheSlot( 
    int slotSize )
        {
            slot_size 
    = slotSize;
            slot_mem 
    = new char [ slot_size ];
            next_slot 
    = NULL;
            last_time 
    = 0;
            file_id 
    = 0;
            locked 
    = 0;
        }

        
    virtual ~e_CacheSlot()
        {
            
    if( slot_mem )
            {
                delete [] slot_mem;
                slot_mem 
    = NULL;
            }
        }

        
    virtual void    deleteThis() = 0;

        
    void    clearThis()
        {
            
    //    backup slot_mem to disk and delete the memory

            
    char    file_name[ MAX_FILE_NAME_LEN ];

            sprintf( file_name, 
    "%d", file_id );
            
            FILE    
    *fp = fopen( file_name, "wb" );

            fwrite( slot_mem, slot_size, 
    1, fp );

            fclose( fp );

            delete [] slot_mem;
            slot_mem 
    = NULL;
        }

        
    void    accessBegin( e_Cache *pCache )
        {
            
    ++ locked;

            
    if( slot_mem == NULL )
            {
                
    //    allocate memory

                slot_mem 
    = new char [ slot_size ];

                
    //    reload data from disk

                
    char    file_name[ MAX_FILE_NAME_LEN ];

                sprintf( file_name, 
    "%d", file_id );
            
                FILE    
    *fp = fopen( file_name, "rb" );

                fread( slot_mem, slot_size, 
    1, fp );

                fclose( fp );

                
    //    delete oldest item

                pCache
    ->purge_memory( slot_size );
            }
        }

        
    void    accessEnd( e_Cache *pCache )
        {
            last_time 
    = pCache->get_current_time();

            
    -- locked;
        }

    public:
        
    char            *slot_mem;

    private:
        e_CacheSlot        
    *next_slot;
        
    int                slot_size;
        ULONG            last_time;
        
    int                file_id;
        
    int                locked;
    };

    //    a cache for a thread, each thread should has its own cache by this design
    class e_Cache {
    public:
        e_Cache( 
    int cacheSize )
        {
            slots 
    = NULL;
            limit 
    = cacheSize;
            current_time 
    = 0;
            current_file_id 
    = 0;
            memory_size 
    = 0;
        }

        
    ~e_Cache()
        {
            e_CacheSlot    
    *pSlot;

            
    while( ( pSlot = slots ) != NULL )
            {
                slots 
    = pSlot->next;

                pSlot
    ->deleteThis();
            }
        }

        ULONG    get_current_time()
        {
            
    return ( ++ current_time );
        }

        
    void    add_slot( e_CacheSlot *newSlot )
        {
            
    if( newSlot )
            {
                newSlot
    ->next_slot = slots;
                slots 
    = newSlot;
                newSlot
    ->file_id = ( ++ current_file_id );

                purge_memory( newSlot
    ->slot_size );
            }
        }

        
    void    purge_memory( int newAddSize )
        {
            memory_size 
    += newAddSize;

            
    //    if there's no enough memory, delete oldest items

            
    if( memory_size > limit && slots )
            {
                e_CacheSlot        
    *oldestSlot = slots;
                e_CacheSlot        
    *pSlot = slots->next_slot;

                
    while( pSlot )
                {
                    
    if( pSlot->locked == 0 && pSlot->last_time < oldestSlot->last_time )
                    {
                        oldestSlot 
    = pSlot;
                    }

                    pSlot 
    = pSlot->next_slot;
                }

                
    if( oldestSlot->locked == 0 )
                {
                    oldestSlot
    ->clearThis();
                    memory_size 
    -= oldestSlot->slot_size;
                }
            }
        }

    private:
        e_CacheSlot        
    *slots;
        
    int                limit;
        ULONG            memory_size;
        ULONG            current_time;
        
    int                current_file_id;
    };
  • 相关阅读:
    Linux中find命令用法全汇总,看完就没有不会用的!
    ubuntu16.04 通过命令,修改屏幕分辨率
    Linux下如何查看哪些进程占用的CPU内存资源最多
    shell脚本 在后台执行de 命令 >> 文件 2>&1 将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)
    ef linq 访问视图返回结果重复
    asp.net core web 本地iis开发
    jQuery控制TR显示隐藏
    mvc EF 从数据库更新实体,添加视图实体时添加不上的问题
    无法确定依赖操作的有效顺序。由于外键约束、模型要求或存储生成的值,因此可能存在依赖关系
    还原差异备份——因为没有文件可用于前滚
  • 原文地址:https://www.cnblogs.com/len3d/p/1290912.html
Copyright © 2011-2022 走看看