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 1 commit
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
72 changes: 69 additions & 3 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,41 @@ 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}";
}

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);
$redisCacheStore = self::initSmallFileRedisInstance();
if ($redisCacheStore)
{
try
{
$result = $redisCacheStore->doGet($cacheKey);
if ($result)
{
$uncompressedResult = gzuncompress($result);
$result = $uncompressedResult ? $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() . ']');
}
}

$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 @@ -252,7 +279,46 @@ public static function file_put_contents ( FileSyncKey $key , $content , $strict
kFile::filePutContents($fullPath , $content);
self::setPermissions($fullPath);
self::createSyncFileForKey($rootPath, $filePath, $key , $strict , !is_null($res), false, md5($content), kPathManager::getStorageProfileIdForKey($key));
self::encryptByFileSyncKey($key, kPathManager::getStorageProfileIdForKey($key));
$encryptedContent = self::encryptByFileSyncKey($key, kPathManager::getStorageProfileIdForKey($key));
$contentToPut = $encryptedContent ? $encryptedContent : $content;
$compressedContentToPut = gzcompress($contentToPut);
$contentToPut = $compressedContentToPut ? $compressedContentToPut : $contentToPut;

//If the file is under a specific threshold in size, save it to redis cache
$contentSize = strlen($contentToPut);
$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 ($contentSize <= $maxFileSizeToCache && in_array("{$key->object_type}_{$key->object_sub_type}", $fileSyncFilesToCache))
{
$redisWrapper = self::initSmallFileRedisInstance();
if ($redisWrapper)
{
$cacheKey = self::getCacheKey($key);
try
{
$fileAdd = $redisWrapper->doSet($cacheKey, $contentToPut);
if ($fileAdd === false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapper doesn't seem to return NULL. Maybe just for in case, it should check if (!$fileAdd)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when testing different redis images, when there was a problem i got an exception there.
i think just in case it better to have it

{
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