Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Redesign the cache internal api to a more composable design structure using enums #623

Open
neon-mmd opened this issue Oct 28, 2024 · 2 comments

Comments

@neon-mmd
Copy link
Owner

What would you like to share?

Work Expected From The Issue

Redesign the cache internal api to move it away from using an OOP based design to a more composable enum based.

The issue expects the following files to be changed/modified:

  • src/cache/cacher.rs

Note

All the files that are expected to be changed are located under the codebase (websurfx directory).

Reason Behind These Changes

The reason behind having these changes is to make the internal API design more flexible and adaptable so that future changes like providing a new caching server as a feature can introduced easily. Additionally, it adopts the rusty-style of code which encourages composability over traditional OOP design.

Sample Design API

The sample API design has been provided below to give a better idea of the work to be done:

///  --- reusing the already existing version ---
#[async_trait]
pub trait CacheTypes: Send + Sync {
    /// ----
}

///  --- reusing the already existing version ---
#[cfg(feature = "memory-cache")]
pub struct InMemoryCache {
    /// -----
}

///  --- reusing the already existing version ---
#[cfg(feature = "memory-cache")]
impl Clone for InMemoryCache {
   /// -----
}

#[cfg(feature = "memory-cache")]
impl InMemoryCache {
    async fn new(config: &Config) -> Self {
       /// ----- 
    }
}

#[cfg(feature = "memory-cache")]
#[async_trait]
impl CacheTypes for InMemoryCache {
    
    ///  --- reusing the already existing version ---
    async fn cached_results(&mut self, url: &str) -> Result<SearchResults, Report<CacheError>> {
       /// ----- 
    }

    ///  --- reusing the already existing version ---
    async fn cache_results(
        &mut self,
        search_results: &[SearchResults],
        urls: &[String],
    ) -> Result<(), Report<CacheError>> {
       /// ----- 
    }
}

/// TODO: New implementation
pub enum SwitchCache {
    #[cfg(feature = "redis-cache")]
    RedisCache(RedisCache),
    #[cfg(feature = "memory-cache")]
    InMemoryCache(InMemoryCache),
}

impl SwitchCache {
    /// TODO: New implementation
    async fn new(config: &Config) -> Result<Self, Box<dyn std::error::Error>> {
        todo();        
    }
}

#[async_trait]
impl CacheTypes for SwitchCache {
    /// TODO: New implementation
    async fn cached_results(&mut self, url: &str) -> Result<SearchResults, Report<CacheError>> {
        todo();
    }

    /// TODO: New implementation
    async fn cache_results(
        &mut self,
        search_results: &[SearchResults],
        urls: &[String],
    ) -> Result<(), Report<CacheError>> {
        todo();
    }
}

///  --- reusing the already existing version ---
pub struct SharedCache(Mutex<SwitchCache>);

///  --- reusing the already existing version ---
impl SharedCache {
    /// ----
}


///  --- reusing the already existing version ---
#[cfg(any(feature = "compress-cache-results", feature = "cec-cache-results"))]
async fn decompress_util(input: &[u8]) -> Result<Vec<u8>, Report<CacheError>> {
    /// ----
}

///  --- reusing the already existing version ---
#[cfg(feature = "redis-cache")]
#[async_trait::async_trait]
impl CacheTypes for RedisCache {
    /// ----
}

///  --- reusing the already existing version ---
impl TryInto<SearchResults> for Vec<u8> {
    /// ----

}

///  --- reusing the already existing version ---
impl TryInto<Vec<u8>> for &SearchResults {
    /// ----
}

Do you want to work on this issue?

None

Additional information

No response

Copy link

The issue has been unlocked and is now ready for dev. If you would like to work on this issue, you can comment to have it assigned to you. You can learn more in our contributing guide https://github.com/neon-mmd/websurfx/blob/rolling/CONTRIBUTING.md

@neon-mmd
Copy link
Owner Author

neon-mmd commented Dec 5, 2024

@gzsombor if you are free and interested. Would you like to work on this one? 😅 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment