## Cache system (Ponchio version)

### Overview

- For each object you create a Token and control its priority and lock
- For each cache you subclass Cache and provide methods for get and drop
- You change priority to each token and call updatePriorities() to make them in effect
- If an object is in a cache is also in all the caches below
- Cache maximum capacity can be surpassed but only by the size of 1 object
- You can lock an object only if it is in the last cache, and once locked it wont be dropped

### Minimal example

    class MyToken: public Token {
     public:
      MyToken(int i): number(i) { setPriority(0); }
      int number;
    };

    class RamCache: public Cache {
     public:
      int get(MyToken *token)  { return 1; }
      int drop(MyToken *token) { return 1; }
      int size(MyToken *token) { return 1; }
    };

    class GpuCache: public Cache {
     public:
      int get(MyToken *token) { return 1; }
      int drop(MyToken *token) { return 1; }
      int size(MyToken *token) { return 1; }
    };

    //generate tokens
    vector tokens;
    for(int i = 0; i < 10000; i++)
    tokens.push_back(MyToken(i));

    //create ram and gpu caches
    RamCache ram;
    ram.setCapacity(5000);
    GpuCache gpu;
    gpu.setCapacity(1000);

    //create controller and add caches
    Controller controller;
    controller.addCache(&ram);
    controller.addCache(&gpu);

    //tell controller about tokens and start
    for(int i = 0; i < tokens.size(); i++) {
      tokens[i].setPriority(rand()/((double)RAND_MAX));
      controller.addToken(&tokens[i]);
    }

    controller.start();

    //change priorities
    for(int i = 0; i < tokens.size(); i++)
      tokens[i].setPriority(rand());
    controller.updatePriorities();

    //lock and use the tokens
    for(int i = 0; i < tokens.size(); i++) {
      bool success = tokens[i].lock();
      if(success) {
      //use the token as you see fit
        tokens[i].unlock();
      }
    }