In memory cache using OO concept

What is a cache in memory? I could not find much information about this from the Internet.

Actually I was asked to create a cache in memory based on the concept of OO using C ++, but I just don’t know how to start. Any suggestions would be appreciated.

+3
source share
3 answers

It depends on the context, but, as a rule, the cache stores a certain value in memory, so it can be restored later, instead of creating a new object. This is most often used in combination with databases - or indeed in any application where building / extracting an object is expensive.

( , !):

class Integer {
    int value;

public:

    Integer(int value) : value(value) {
        sleep(1000); // Simulates an expensive constructor
    }
};

, :

Integer one(1);
Integer two(2);
// etc.

... ( ), , 2:

Integer two(2);

. , ? , , factory :

class Integer {
    int value;

    static std::map<int, Integer> cache;

    Integer(int value) : value(value) {
        sleep(1000); // Simulates an expensive constructor
    }

    friend Integer make_int(int);
};

Integer make_int(int value) {
    std::map<int, Integer>::iterator i = Integer::cache.find(value);
    if (i != Integer::cache.end())
        return i->second;

    Integer ret = Integer(value);
    Integer::cache[value] = ret;
    return ret;
}

make_int . :

Integer one = make_int(1);
Integer two = make_int(2);
Integer other = make_int(2); // Recycles instance from above.
+5

, , , , HTTP- . , LRU. , , , , , , . , , .

, "" . , , , . , , , . , (, DAO), .

0

memcached ++ API , :

, , , - .

Memcached is a key-value in memory to store small pieces of arbitrary data (rows, objects) from the results of database calls, API calls, or a rendering page.

0
source

Source: https://habr.com/ru/post/1796109/


All Articles