1// Copyright 2020 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package git
6
7import (
8	"crypto/sha256"
9	"fmt"
10
11	"code.gitea.io/gitea/modules/log"
12)
13
14// Cache represents a caching interface
15type Cache interface {
16	// Put puts value into cache with key and expire time.
17	Put(key string, val interface{}, timeout int64) error
18	// Get gets cached value by given key.
19	Get(key string) interface{}
20}
21
22func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string {
23	hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", repoPath, ref, entryPath)))
24	return fmt.Sprintf("last_commit:%x", hashBytes)
25}
26
27// Put put the last commit id with commit and entry path
28func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
29	if c == nil || c.cache == nil {
30		return nil
31	}
32	log.Debug("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
33	return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl())
34}
35