package com.atlassian.bitbucket.mesh.git.cache;

import com.atlassian.bitbucket.mesh.repository.Repository;
import com.atlassian.util.contentcache.CacheStatistics;
import com.atlassian.util.contentcache.ContentCache;
import com.atlassian.util.contentcache.ContentCacheStatistics;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Date;

/**
 * Service to control the Pack cache.
 */
public interface PackCacheService {

    /**
     * Clears the cache. All cached responses will be deleted.
     */
    void clear();

    /**
     * Clears the cache for a repository.
     * @param repository the repository
     */
    void clear(@Nonnull Repository repository);

    /**
     * Clears an individual entry from the cache. Calling this method for an entry that does not exist in the cache
     * will have no effect.
     *
     * @param repository the repository
     * @param cacheKey the cacheKey for the repository
     */
    void clear(@Nonnull Repository repository, @Nonnull String cacheKey);

    /**
     * Convenience method for constructing an expiry date.
     *
     * @return  {@code null} if no time to live has been configured. Otherwise a {@link Date} that is ttl in the future
     *          from now.
     */
    @Nullable
    Date createExpiryDate();

    /**
     * @param repository the repository to retrieve a cache for
     * @return the repository's cache
     */
    @Nonnull
    ContentCache getCache(@Nonnull Repository repository);

    /**
     * @return the maximum number of cache entries to retain (per repository)
     */
    int getMaxCount();

    /**
     * @return cache statistics, including the total size of all the caches, the number of hits and misses, etc.
     */
    @Nonnull
    CacheStatistics getStatistics();

    /**
     * @return cache statistics for a repository, including the total size of all the caches, the number of hits, etc.
     * @param repository the {@link Repository} for which the statistics are to be returned
     */
    @Nullable
    ContentCacheStatistics getStatistics(@Nonnull Repository repository);

    /**
     * @return whether caching is enabled for the HTTP protocol.
     */
    boolean isEnabledForHttp();

    /**
     * @return whether caching is enabled for the SSH protocol.
     */
    boolean isEnabledForSsh();
}
