github.com/polarismesh/polaris@v1.17.8/cache/api/funcs.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package api
    19  
    20  import (
    21  	"crypto/sha1"
    22  	"encoding/hex"
    23  	"hash"
    24  	"sort"
    25  	"time"
    26  )
    27  
    28  const mtimeLogIntervalSec = 120
    29  
    30  // LogLastMtime 定时打印mtime更新结果
    31  func LogLastMtime(lastMtimeLogged int64, lastMtime int64, prefix string) int64 {
    32  	curTimeSec := time.Now().Unix()
    33  	if lastMtimeLogged == 0 || curTimeSec-lastMtimeLogged >= mtimeLogIntervalSec {
    34  		lastMtimeLogged = curTimeSec
    35  		log.Infof("[Cache][%s] current lastMtime is %s", prefix, time.Unix(lastMtime, 0))
    36  	}
    37  	return lastMtimeLogged
    38  }
    39  
    40  func ComputeRevisionBySlice(h hash.Hash, slice []string) (string, error) {
    41  	for _, revision := range slice {
    42  		if _, err := h.Write([]byte(revision)); err != nil {
    43  			return "", err
    44  		}
    45  	}
    46  	return hex.EncodeToString(h.Sum(nil)), nil
    47  }
    48  
    49  // CompositeComputeRevision 将多个 revision 合并计算为一个
    50  func CompositeComputeRevision(revisions []string) (string, error) {
    51  	h := sha1.New()
    52  
    53  	sort.Strings(revisions)
    54  
    55  	for i := range revisions {
    56  		if _, err := h.Write([]byte(revisions[i])); err != nil {
    57  			return "", err
    58  		}
    59  	}
    60  
    61  	return hex.EncodeToString(h.Sum(nil)), nil
    62  }