Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caching of small files in redis #13148

Open
wants to merge 8 commits into
base: Ursa-21.11.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alpha/apps/kaltura/lib/cache/kCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class kCacheManager
// cache types
const CACHE_TYPE_PLAY_MANIFEST = 'playManifest';
const CACHE_TYPE_FILE_SYNC = 'fileSync';
const CACHE_TYPE_SMALL_FILE_SYNC = 'smallFileSync';
const CACHE_TYPE_PERMISSION_MANAGER = 'permissionManager';
const CACHE_TYPE_QUERY_CACHE_KEYS = 'queryCacheKeys';
const CACHE_TYPE_QUERY_CACHE_QUERIES = 'queryCacheQueries';
Expand Down
90 changes: 88 additions & 2 deletions alpha/apps/kaltura/lib/storage/kFileSyncUtils.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class kFileSyncUtils implements kObjectChangedEventConsumer, kObjectAddedEventCo

const SOURCE_TYPE_FILE = 'file';
const SOURCE_TYPE_HTTP = 'http';
const FILE_SYNC_TYPES_TO_CACHE = 'file_sync_types_to_cache';
const MAX_FILE_SIZE_TO_CACHE = 'max_file_size_to_cache';

/**
* Contain all object types and sub types that should not be synced
Expand Down Expand Up @@ -170,16 +172,53 @@ public static function dir_get_files(FileSyncKey $key, $strict = true)
return $files;
}

protected static function getCacheKey(FileSyncKey $key)
{
return self::CACHE_KEY_PREFIX . "{$key->object_id}_{$key->object_type}_{$key->object_sub_type}_{$key->version}";
}

private static function getFromSmallFileCache($cacheKey)
{
$redisCacheStore = self::initSmallFileRedisInstance();
if (!$redisCacheStore)
{
return;
}

try
{
$result = $redisCacheStore->doGet($cacheKey);
if ($result)
{
$uncompressedResult = gzuncompress($result);
$result = $uncompressedResult ?? $result;
KalturaLog::info("returning from redis cache, key [$cacheKey] size [" . strlen($result) . "]");
return $result;
}
}
catch(Exception $e)
{
KalturaLog::info('File not found on Redis cache. [' . $e->getCode() . ' : ' . $e->getMessage() . ']');
}
}

public static function file_get_contents ( FileSyncKey $key , $fetch_from_remote_if_no_local = true , $strict = true , $max_file_size = 0 )
{
$cacheKey = self::getCacheKey($key);

$resultFromSmallCache = self::getFromSmallFileCache($cacheKey);
if ($resultFromSmallCache)
{
return $resultFromSmallCache;
}

$cacheStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_FILE_SYNC);
if ($cacheStore)
{
$cacheKey = self::CACHE_KEY_PREFIX . "{$key->object_id}_{$key->object_type}_{$key->object_sub_type}_{$key->version}";
$result = $cacheStore->get($cacheKey);
if ($result)
{
KalturaLog::info("returning from cache, key [$cacheKey] size [".strlen($result)."]");
KalturaLog::info("returning from cache, key [$cacheKey] size [" . strlen($result) . "]");
return $result;
}
}
Expand Down Expand Up @@ -253,6 +292,53 @@ public static function file_put_contents ( FileSyncKey $key , $content , $strict
self::setPermissions($fullPath);
self::createSyncFileForKey($rootPath, $filePath, $key , $strict , !is_null($res), false, md5($content), kPathManager::getStorageProfileIdForKey($key));
self::encryptByFileSyncKey($key, kPathManager::getStorageProfileIdForKey($key));
self::setInSmallFileCache($key, $content);
}

protected static function setInSmallFileCache($key, $content)
{
$fileSyncFilesToCache = kConf::get(self::FILE_SYNC_TYPES_TO_CACHE, kConfMapNames::RUNTIME_CONFIG, array());
$maxFileSizeToCache = kConf::get(self::MAX_FILE_SIZE_TO_CACHE, kConfMapNames::RUNTIME_CONFIG);
if (!$fileSyncFilesToCache || !$maxFileSizeToCache || !in_array("{$key->object_type}_{$key->object_sub_type}", $fileSyncFilesToCache))
{
return;
}

$contentToPut = gzcompress($content) ?? $content;
$contentSize = strlen($contentToPut);
if ($contentSize <= $maxFileSizeToCache)
{
$redisWrapper = self::initSmallFileRedisInstance();
if (!$redisWrapper)
{
return;
}

$cacheKey = self::getCacheKey($key);
try
{
$fileAdd = $redisWrapper->doSet($cacheKey, $contentToPut);
if ($fileAdd === false)
{
KalturaLog::err("Failed to add file content with key [$key]");
}
}
catch (Exception $e)
{
KalturaLog::err("Failed to add file content with key [$key] - [" . $e->getCode() . " : " . $e->getMessage() . "]");
}
}
}

protected static function initSmallFileRedisInstance()
{
$redisWrapper = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_SMALL_FILE_SYNC);
if (!$redisWrapper)
{
return null;
}

return $redisWrapper;
}

protected static function setPermissions($filePath)
Expand Down