forked from TencentBlueKing/bk-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'TencentBlueKing:master' into pref-9366
- Loading branch information
Showing
16 changed files
with
389 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...mmon/common-api/src/main/kotlin/com/tencent/devops/common/api/cache/BkDiskLruFileCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.tencent.devops.common.api.cache | ||
|
||
import com.jakewharton.disklrucache.DiskLruCache | ||
import com.tencent.devops.common.api.util.ShaUtils | ||
import org.slf4j.LoggerFactory | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
|
||
/** | ||
* 蓝盾文件磁盘缓存 | ||
*/ | ||
class BkDiskLruFileCache( | ||
private val cacheDir: String, | ||
private val cacheSize: Long | ||
) { | ||
|
||
private val diskCache: DiskLruCache = DiskLruCache.open(File(cacheDir), 1, 1, cacheSize) | ||
|
||
companion object { | ||
private const val BUFFER_SIZE = 1024 | ||
private val logger = LoggerFactory.getLogger(BkDiskLruFileCache::class.java) | ||
} | ||
|
||
/** | ||
* 把文件放入磁盘缓存 | ||
* @param key 磁盘缓存key | ||
* @param inputFile 缓存文件 | ||
*/ | ||
@Throws(IOException::class) | ||
fun put(key: String, inputFile: File) { | ||
// 根据key获取缓存编辑器(为了保证key格式符合DiskLruCache规范,key需要用sha算法计算出散列值进行转换) | ||
val editor = diskCache.edit(ShaUtils.sha256(key)) ?: return | ||
// 如果编辑器不为空,把文件写入磁盘缓存 | ||
FileInputStream(inputFile).use { inputStream -> | ||
editor.newOutputStream(0).use { outputStream -> | ||
val buffer = ByteArray(BUFFER_SIZE) | ||
var bytesRead: Int | ||
while (inputStream.read(buffer).also { bytesRead = it } != -1) { | ||
outputStream.write(buffer, 0, bytesRead) | ||
} | ||
} | ||
} | ||
editor.commit() | ||
// 手动触发淘汰策略 | ||
diskCache.flush() | ||
} | ||
|
||
/** | ||
* 从磁盘缓存中获取文件到指定位置 | ||
* @param key 磁盘缓存key | ||
* @param outputFile 输出文件 | ||
*/ | ||
@Throws(IOException::class) | ||
fun get(key: String, outputFile: File) { | ||
// 根据key从磁盘缓存获取snapshot对象 | ||
val snapshot = diskCache[ShaUtils.sha256(key)] | ||
snapshot?.getInputStream(0)?.use { inputStream -> | ||
// 将snapshot对象输出流写入输出文件 | ||
FileOutputStream(outputFile).use { outputStream -> | ||
val buffer = ByteArray(BUFFER_SIZE) | ||
var bytesRead: Int | ||
while (inputStream.read(buffer).also { bytesRead = it } != -1) { | ||
outputStream.write(buffer, 0, bytesRead) | ||
} | ||
} | ||
} | ||
// 将文件设置为可执行文件 | ||
if (outputFile.exists()) { | ||
val success = outputFile.setExecutable(true) | ||
if (success) { | ||
logger.info("file[${outputFile.absolutePath}] execution permission added successfully.") | ||
} else { | ||
logger.warn("file[${outputFile.absolutePath}] failed to add execution permission.") | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 从磁盘缓存中移除缓存对象 | ||
* @param key 磁盘缓存key | ||
*/ | ||
@Throws(IOException::class) | ||
fun remove(key: String) { | ||
diskCache.remove(ShaUtils.sha256(key)) | ||
} | ||
|
||
/** | ||
* 关闭缓存 | ||
*/ | ||
@Throws(IOException::class) | ||
fun close() { | ||
diskCache.close() | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...on-api/src/main/kotlin/com/tencent/devops/common/api/factory/BkDiskLruFileCacheFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. | ||
* | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. | ||
* | ||
* A copy of the MIT License is included in this file. | ||
* | ||
* | ||
* Terms of the MIT License: | ||
* --------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.tencent.devops.common.api.factory | ||
|
||
import com.tencent.devops.common.api.cache.BkDiskLruFileCache | ||
|
||
object BkDiskLruFileCacheFactory { | ||
|
||
fun getDiskLruFileCache( | ||
cacheDir: String, | ||
cacheSize: Long | ||
): BkDiskLruFileCache { | ||
return BkDiskLruFileCache(cacheDir, cacheSize) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.