An open API service indexing awesome lists of open source software.

https://github.com/h2337/cppdataloader

cppdataloader is a batching and caching library for C++17
https://github.com/h2337/cppdataloader

batch batching cache caching cpp cpp17 dataloader

Last synced: 8 months ago
JSON representation

cppdataloader is a batching and caching library for C++17

Awesome Lists containing this project

README

          

# cppdataloader

cppdataloader is a batching and caching library for C++17.

cppdataloader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.

## Features
- [x] Batching
- [x] Caching
- [x] Direct dispatch
- [ ] Timeout dispatch
- [ ] Empty values

## Quickstart

cppq is a header-only library with no dependencies.

Just include the header: `#include "cppdataloader.h"`.

## Example

```c++
#include "cppdataloader.hpp"

#include

class User {
public:
User(long _id): id(_id) {}
long id;
};

class UserBatchLoader : public BatchLoader {
public:
std::vector load(std::vector userIds) const override {
auto result = std::vector();
// Make a DB call here or something and pass in userIds to SELECT and return
for (auto userId : userIds)
result.push_back(User(userId));
// Results should be in the same order as passed-in IDs
return result;
}
};

int main(int argc, char *argv[]) {
UserBatchLoader userBatchLoader = UserBatchLoader();
DataLoader userLoader = DataLoader(userBatchLoader);

std::future load1 = userLoader.load(1);
std::future load2 = userLoader.load(2);
// Since this fetches the same ID of `1`, it will return the cached value
std::future load3 = userLoader.load(1);

// Both calls to `load` will be batched and executed with this call
userLoader.dispatch();

std::cout << load1.get().id << " " << load2.get().id << " " << load3.get().id << std::endl;
}
```

## License

cppdataloader is MIT-licensed.