gitai/remote/cache/
key_generator.rs

1use std::hash::{DefaultHasher, Hash, Hasher};
2
3use crate::remote::models::repo_config::RepositoryConfiguration;
4
5pub struct CacheKeyGenerator;
6
7impl CacheKeyGenerator {
8    /// Generate a unique cache key for a repository configuration
9    /// The key is based on the repository URL and branch
10    pub fn generate_key(config: &RepositoryConfiguration) -> String {
11        let mut hasher = DefaultHasher::new();
12
13        // Hash the URL and branch to create a unique key
14        config.url.hash(&mut hasher);
15        config.branch.hash(&mut hasher);
16
17        // If commit hash is specified, include it in the key
18        if let Some(ref commit) = config.commit_hash {
19            commit.hash(&mut hasher);
20        }
21
22        let hash = hasher.finish();
23        format!("{hash:x}")
24    }
25
26    /// Generate a cache key specifically for the URL and branch
27    pub fn generate_url_branch_key(url: &str, branch: &str) -> String {
28        let mut hasher = DefaultHasher::new();
29        url.hash(&mut hasher);
30        branch.hash(&mut hasher);
31
32        let hash = hasher.finish();
33        format!("{hash:x}")
34    }
35}