pub struct PeerCache {
pub users: HashMap<i64, i64>,
pub channels: HashMap<i64, (i64, Option<ChannelKind>)>,
pub communities: HashMap<i64, i64>,
pub chats: HashSet<i64>,
pub channels_min: HashSet<i64>,
pub min_contexts: HashMap<i64, (i64, i32)>,
pub username_to_peer: HashMap<String, (i64, PeerType)>,
pub phone_to_user: HashMap<String, i64>,
/* private fields */
}Expand description
All fields are pub so that save_session / connect can read/write them
directly, and so that advanced callers can inspect the cache.
Fields§
§users: HashMap<i64, i64>user_id -> access_hash (full users only, min=false)
channels: HashMap<i64, (i64, Option<ChannelKind>)>channel_id -> (access_hash, Option<ChannelKind>) (full channels only, min=false)
communities: HashMap<i64, i64>community_id -> access_hash (full communities only, min=false).
Kept separate from channels: a Community is a distinct Chat variant
with no ChannelKind, even though it resolves to the same
InputPeer::Channel shape on the wire.
chats: HashSet<i64>Regular group chat IDs (Chat::Chat / ChatForbidden). Groups need no access_hash; track existence for peer validation.
channels_min: HashSet<i64>Channel IDs seen with min=true. These are real channels but have no
valid access_hash. Stored separately so they are NEVER confused with
regular groups. DO NOT put min channels in chats. A min channel must
never become InputPeerChat - that causes fatal RPC failures.
min_contexts: HashMap<i64, (i64, i32)>user_id -> (peer_id, msg_id) for min users seen in a message context. Min users have an invalid access_hash; they must be referenced via InputPeerUserFromMessage using the peer and message where they appeared.
username_to_peer: HashMap<String, (i64, PeerType)>Reverse index: lowercase username → (id, PeerType). Populated by cache_user / cache_chat; always overwritten on update (usernames can change).
phone_to_user: HashMap<String, i64>Reverse index: E.164 phone → user_id.
Implementations§
Source§impl PeerCache
impl PeerCache
Sourcepub fn new(experimental: ExperimentalFeatures) -> Self
pub fn new(experimental: ExperimentalFeatures) -> Self
Create a new empty cache with the given experimental-feature flags.
pub fn cache_user(&mut self, user: &User)
Sourcepub fn cache_user_with_context(
&mut self,
user: &User,
peer_id: i64,
msg_id: i32,
)
pub fn cache_user_with_context( &mut self, user: &User, peer_id: i64, msg_id: i32, )
Cache a user that arrived in a message context.
For min users (access_hash is invalid), stores the peer+msg context so
they can later be referenced via InputPeerUserFromMessage.
Uses latest-wins semantics: a newer message context replaces the stored one. Recent messages are less likely to have been deleted.
pub fn cache_chat(&mut self, chat: &Chat)
Sourcepub fn channel_kind_of(&self, channel_id: i64) -> Option<ChannelKind>
pub fn channel_kind_of(&self, channel_id: i64) -> Option<ChannelKind>
Look up the cached ChannelKind for a channel ID.
Returns None when the channel is not in the cache or was loaded from a
pre-v6 session file that predates kind tracking.
pub fn cache_users(&mut self, users: &[User])
pub fn cache_chats(&mut self, chats: &[Chat])
Sourcepub fn cache_input_peer(&mut self, ip: &InputPeer)
pub fn cache_input_peer(&mut self, ip: &InputPeer)
Store an already-resolved InputPeer’s access hash into the cache.
Called when a caller provides a PeerRef::Input so that the subsequent
peer_to_input lookup succeeds without an RPC.
Sourcepub fn invalidate_peer(&mut self, peer: &Peer)
pub fn invalidate_peer(&mut self, peer: &Peer)
Remove stale cache entries when Telegram rejects them with
PEER_ID_INVALID, CHANNEL_INVALID, USER_ID_INVALID, or
CHANNEL_PRIVATE. The caller should then retry the operation.
Sourcepub fn clear_min_contexts(&mut self)
pub fn clear_min_contexts(&mut self)
Drop all cached min-user contexts.
Safe to call at any time. After this, any min user that has no full
access_hash returns InvocationError::PeerNotCached until they
appear again in an update (and cache_min_peers is enabled) or until
resolve_peer() fetches their full hash.
Call this periodically on gateway servers to bound memory growth from min-user entries that accumulate over long uptimes.
Sourcepub fn stats(&self) -> PeerCacheStats
pub fn stats(&self) -> PeerCacheStats
A point-in-time snapshot of entry counts in this cache.
Cheap to call, no allocation beyond the returned struct itself.