drogonframework / drogonframework/drogon

Allow `Session` to be `std::any`

Open
#1,853 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
14.3k
Forks
1.4k
Avg merge
1d 13h
Merged PRs (30d)
14

Description

**Is your feature request related to a problem? Please describe.**
The developer may want to attach an object to the session, instead of using key-value pairs.
There is a map lookup cost currently, if it becomes `std::any`, then the object can be directly retrieved without the extra lookup.
How it is done now:
```c++
UserPtr user = ...;
req->session()->insert("", user);

// Later when accessed
UserPtr user = req->session()->get("");
notify(user->speed());
```
Now we could have the `User`'s members as keys and values in the session instead, but this still needs a lookup for each member access:
```c++
auto &session = req->session();
session->insert("logged-in", true);
session->insert("speed", 0.0);

// Later when accessed
double speed = req->session()->get("speed");
notify(speed);
```

If it is changed to `std::any`:
```c++
UserPtr user = ...;
req->session()->setContext(user);

// Later when accessed
UserPtr user = req->session()->getContext();
notify(user->speed());
```
No lookup was needed, it is a straight cast of `std::any` to `UserPtr`.

**Describe the solution you'd like**
Sessions can be similar to `WebSocketConnection::contextPtr_`. We can keep backward compatibility by having that `std::any` member set to `std::map` by default.

**Describe alternatives you've considered**
Custom rewritten logic for sessions.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.