# concurrent_lru_cache Template Class

## Summary

Template class for Least Recently Used cache with concurrent operations.

## Syntax

    template <typename key_type, typename value_type, typename value_functor_type = value_type (*)(key_type) >
     class concurrent_lru_cache;

## Header

    #define TBB_PREVIEW_CONCURRENT_LRU_CACHE 1
     #include "tbb/concurrent_lru_cache.h"

## Description

A concurrent_lru_cache container maps keys to values with the ability to limit the number of stored unused objects. There is at most one element in the container for each key.

The container permits multiple threads to concurrently retrieve items from it.

The container tracks the lifetime of retrieved items by returning a proxy object instead of a real value.

The container stores all the items that are currently in use and a limited number of unused items. Extra items are removed in a least recently used manner.

When no item is found for a key, the container calls the user provided function object to get a needed value and inserts it. The functor object must be thread safe.

## Members

    namespace tbb {
     template <typename key_type,
     typename value_type,
     typename value_functor_type = value_type (*)(key_type) >
     class concurrent_lru_cache{
     private:
     class handle_object;
     public:
     typedef handle_object handle;
     public:
     concurrent_lru_cache(value_functor_type f,std::size_t number_of_lru_history_items);
     handle_object operator()(key_type k);
     private:
     struct handle_move_t;
     class handle_object {
     public:
     handle_object(handle_move_t m);
     operator handle_move_t();
     value_type& value();
     ~handle_object();
     friend handle_move_t move(handle_object& h);
     private:
     void operator=(handle_object&);
     handle_object(handle_object &);
     };
    };
     }

The following table provides additional information on the members of this template class.
Member | Description
--- | ---
concurrent_lru_cache(value_function_type f,std::size_t number_of_lru_history_items); | Constructs an empty cache with a `number_of_lru_history_items` maximum number of stored unused objects, and `f` function object returning new values.
handle_object operator[](key_type k) | Search the container for a pair with given key. If the pair is not found, the user provided function object is called to get the value and insert it into the container. Returns: `handle_object` pointing to the matching value.
~ concurrent_lru_cache () | Destroys all items in the container, and the container itself, so that it can no longer be used.
handle_object class | More Info
handle_move_t class | This is an instrumental class to allow certain conversions that allow ownership transfer between instances of `handle_object` objects. As well it allows `handle_object` objects to be passed to and returned from functions. The class has no members other than holding a reference to value object in LRU cache container and a pointer to the container itself.

Parent topic: Community Preview Features

- handle_object class