modify: file cache writer md5 to sha256 (#5727)
* modify: file cache writer md5 to sha256 * modify: file cache writer md5 to sha256 * modify: file cache writer md5 to sha256
This commit is contained in:
parent
bb72dc27ac
commit
e7fa4835f7
14
client/cache/file.go
vendored
14
client/cache/file.go
vendored
@ -17,7 +17,7 @@ package cache
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/gob"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
@ -123,21 +123,22 @@ func (fc *FileCache) Init() error {
|
||||
|
||||
// getCacheFileName returns a md5 encoded file name.
|
||||
func (fc *FileCache) getCacheFileName(key string) (string, error) {
|
||||
m := md5.New()
|
||||
m := sha256.New()
|
||||
_, _ = io.WriteString(m, key)
|
||||
keyMd5 := hex.EncodeToString(m.Sum(nil))
|
||||
keySha256 := hex.EncodeToString(m.Sum(nil))
|
||||
cachePath := fc.CachePath
|
||||
switch fc.DirectoryLevel {
|
||||
case 2:
|
||||
cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4])
|
||||
cachePath = filepath.Join(cachePath, keySha256[0:2], keySha256[2:4])
|
||||
case 1:
|
||||
cachePath = filepath.Join(cachePath, keyMd5[0:2])
|
||||
cachePath = filepath.Join(cachePath, keySha256[0:2])
|
||||
}
|
||||
ok, err := exists(cachePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !ok {
|
||||
fmt.Printf("cachePath: %s\n", cachePath)
|
||||
err = os.MkdirAll(cachePath, os.ModePerm)
|
||||
if err != nil {
|
||||
return "", berror.Wrapf(err, CreateFileCacheDirFailed,
|
||||
@ -145,7 +146,7 @@ func (fc *FileCache) getCacheFileName(key string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, fc.FileSuffix)), nil
|
||||
return filepath.Join(cachePath, fmt.Sprintf("%s%s", keySha256, fc.FileSuffix)), nil
|
||||
}
|
||||
|
||||
// Get value from file cache.
|
||||
@ -212,6 +213,7 @@ func (fc *FileCache) Put(ctx context.Context, key string, val interface{}, timeo
|
||||
}
|
||||
|
||||
fn, err := fc.getCacheFileName(key)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
10
client/cache/file_test.go
vendored
10
client/cache/file_test.go
vendored
@ -17,11 +17,10 @@ package cache
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFileCacheStartAndGC(t *testing.T) {
|
||||
@ -30,6 +29,8 @@ func TestFileCacheStartAndGC(t *testing.T) {
|
||||
assert.NotNil(t, err)
|
||||
err = fc.StartAndGC(`{}`)
|
||||
assert.Nil(t, err)
|
||||
_, err = fc.getCacheFileName("key1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, fc.CachePath, FileCachePath)
|
||||
assert.Equal(t, fc.DirectoryLevel, FileCacheDirectoryLevel)
|
||||
@ -47,12 +48,17 @@ func TestFileCacheStartAndGC(t *testing.T) {
|
||||
assert.Equal(t, fc.DirectoryLevel, 2)
|
||||
assert.Equal(t, fc.EmbedExpiry, 0)
|
||||
assert.Equal(t, fc.FileSuffix, ".bin")
|
||||
_, err = fc.getCacheFileName("key1")
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"aaa","EmbedExpiry":"0"}`, str))
|
||||
assert.NotNil(t, err)
|
||||
|
||||
err = fc.StartAndGC(fmt.Sprintf(`{"CachePath":"%s","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"aaa"}`, str))
|
||||
assert.NotNil(t, err)
|
||||
|
||||
_, err = fc.getCacheFileName("key1")
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestFileCacheInit(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user