New caching mechanic

Source: Internet
Author: User
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 clearis ()
{
// 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 accessential D (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 shoshould 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-> clearthur is ();
Memory_size-= oldestslot-> slot_size;
}
}
}

PRIVATE:
E_cacheslot * slots;
Int limit;
Ulong memory_size;
Ulong current_time;
Int current_file_id;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.