Cache system (Ponchio version)

Overview

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();
  }
}