init
This commit is contained in:
		
							
								
								
									
										46
									
								
								Kreta.BusinessLogic/Caching/ElearningCache.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								Kreta.BusinessLogic/Caching/ElearningCache.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Kreta.BusinessLogic.HelperClasses;
 | 
			
		||||
using Kreta.Core.Elearning.Nexius;
 | 
			
		||||
using Kreta.Framework.Caching;
 | 
			
		||||
using Nexius.IdentityProvider.Model.External.V1;
 | 
			
		||||
 | 
			
		||||
namespace Kreta.KretaServer.Caching
 | 
			
		||||
{
 | 
			
		||||
    public class ElearningCache : GenericCache<IDictionary<string, KretaCourseCo>>
 | 
			
		||||
    {
 | 
			
		||||
        private static readonly string ElearningCacheKey = $"{nameof(Kreta)}_{nameof(ElearningCache)}";
 | 
			
		||||
 | 
			
		||||
        public ElearningCache(Framework.Caching.CacheManager cacheManager) : base(cacheManager, nameof(ElearningCache))
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public IDictionary<string, Course> GetCourses(IEnumerable<string> idList, INexiusCourseService nexiusCourseService)
 | 
			
		||||
        {
 | 
			
		||||
            var allCourses = GetAllCourses(nexiusCourseService);
 | 
			
		||||
            var result = new Dictionary<string, Course>();
 | 
			
		||||
            foreach (var id in idList)
 | 
			
		||||
            {
 | 
			
		||||
                if (allCourses.TryGetValue(id.ToLower(), out KretaCourseCo co))
 | 
			
		||||
                {
 | 
			
		||||
                    result.Add(id.ToLower(), co.ToCourse());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return result;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private IDictionary<string, KretaCourseCo> GetAllCourses(INexiusCourseService nexiusCourseService)
 | 
			
		||||
        {
 | 
			
		||||
            return Get(ElearningCacheKey) ?? LoadAllCourses(nexiusCourseService);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private IDictionary<string, KretaCourseCo> LoadAllCourses(INexiusCourseService nexiusCourseService)
 | 
			
		||||
        {
 | 
			
		||||
            var courseDictionary = nexiusCourseService.GetAllCourses().ToDictionary(c => c.Id.ToString().ToLower(), c => new KretaCourseCo(c));
 | 
			
		||||
            AddOrUpdate(ElearningCacheKey, courseDictionary, x => courseDictionary);
 | 
			
		||||
 | 
			
		||||
            return courseDictionary;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										64
									
								
								Kreta.BusinessLogic/Caching/FileServiceCache.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								Kreta.BusinessLogic/Caching/FileServiceCache.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
using System;
 | 
			
		||||
using CacheManager.Core;
 | 
			
		||||
using Kreta.BusinessLogic.Interfaces;
 | 
			
		||||
using Kreta.Core;
 | 
			
		||||
using Kreta.Framework.Caching;
 | 
			
		||||
 | 
			
		||||
namespace Kreta.BusinessLogic.Caching
 | 
			
		||||
{
 | 
			
		||||
    public class FileServiceCache : GenericCache<string>
 | 
			
		||||
    {
 | 
			
		||||
        private static readonly string PublicTokenCache = $"{Constants.Cache.CacheKeyPrefix}{nameof(PublicTokenCache)}";
 | 
			
		||||
 | 
			
		||||
        private static readonly string PrivateTokenCache = $"{Constants.Cache.CacheKeyPrefix}{nameof(PrivateTokenCache)}";
 | 
			
		||||
 | 
			
		||||
        public FileServiceCache(Framework.Caching.CacheManager cacheManager) : base(cacheManager, nameof(FileServiceCache)) { }
 | 
			
		||||
 | 
			
		||||
        public string GetPublicToken(ITokenServiceHelper tokenServiceHelper, bool forceUpdate = false)
 | 
			
		||||
            => !forceUpdate
 | 
			
		||||
                ? Get(PublicTokenCache) ?? SetPublicTokenFromService(tokenServiceHelper)
 | 
			
		||||
                : SetPublicTokenFromService(tokenServiceHelper);
 | 
			
		||||
 | 
			
		||||
        public string GetPrivateToken(ITokenServiceHelper tokenServiceHelper, bool forceUpdate = false)
 | 
			
		||||
            => !forceUpdate
 | 
			
		||||
                ? Get(PrivateTokenCache) ?? SetPrivateTokenFromService(tokenServiceHelper)
 | 
			
		||||
                : SetPrivateTokenFromService(tokenServiceHelper);
 | 
			
		||||
 | 
			
		||||
        private string SetPrivateTokenFromService(ITokenServiceHelper tokenServiceHelper)
 | 
			
		||||
        {
 | 
			
		||||
            var (token, expiresIn) = tokenServiceHelper.GetPrivateTokenWithExpiresIn();
 | 
			
		||||
 | 
			
		||||
            AddTokentToCache(PrivateTokenCache, token, expiresIn);
 | 
			
		||||
 | 
			
		||||
            return token;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private string SetPublicTokenFromService(ITokenServiceHelper tokenServiceHelper)
 | 
			
		||||
        {
 | 
			
		||||
            var (token, expiresIn) = tokenServiceHelper.GetPublicTokenWithExpiresIn();
 | 
			
		||||
 | 
			
		||||
            AddTokentToCache(PublicTokenCache, token, expiresIn);
 | 
			
		||||
 | 
			
		||||
            return token;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void AddTokentToCache(string key, string token, int expiresIn)
 | 
			
		||||
        {
 | 
			
		||||
            AddOrUpdate(key, token, oldValue => token);
 | 
			
		||||
            Expire(key, ExpirationMode.Absolute, GetExpirationTime(expiresIn));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private TimeSpan GetExpirationTime(int expiresIn)
 | 
			
		||||
        {
 | 
			
		||||
            var timeout = new TimeSpan(0, 0, expiresIn);
 | 
			
		||||
            var offsetTime = new TimeSpan(0, 1, 0);
 | 
			
		||||
 | 
			
		||||
            if (timeout > offsetTime)
 | 
			
		||||
            {
 | 
			
		||||
                timeout -= offsetTime;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return timeout;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										67
									
								
								Kreta.BusinessLogic/Caching/TantargyiAtlagCache.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								Kreta.BusinessLogic/Caching/TantargyiAtlagCache.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Kreta.Core;
 | 
			
		||||
using Kreta.Ellenorzo.Domain.VN.Ertekeles.Atlag.TantargyiAtlag;
 | 
			
		||||
using Kreta.Framework.Caching;
 | 
			
		||||
 | 
			
		||||
namespace Kreta.BusinessLogic.Caching
 | 
			
		||||
{
 | 
			
		||||
    public class TantargyiAtlagCache : GenericCache<object>
 | 
			
		||||
    {
 | 
			
		||||
        private static readonly string TantargyiAtlagCacheKeyPrefix = $"{Constants.Cache.CacheKeyPrefix}TantargyiAtlagCache_";
 | 
			
		||||
        private static readonly string TantargyiAtlagRegionPrefix = $"{Constants.Cache.CacheKeyPrefix}TantargyiAtlagCacheRegion_";
 | 
			
		||||
        private static readonly string TantargyiAtlagTriggerPrefix = $"{Constants.Cache.CacheKeyPrefix}TantargyiAtlagTrigger_";
 | 
			
		||||
 | 
			
		||||
        public TantargyiAtlagCache(Framework.Caching.CacheManager cacheManager) : base(cacheManager, nameof(TantargyiAtlagCache))
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private string GetCacheKey(string keyPrefix, int userId, string instituteCode, string tantargyUid, int? oktatasiNevelesiFeladatId)
 | 
			
		||||
        {
 | 
			
		||||
            return $"{keyPrefix}{instituteCode}.{userId}.{tantargyUid}.{oktatasiNevelesiFeladatId}";
 | 
			
		||||
        }
 | 
			
		||||
        private static string GetTantargyiAtlagRegionKey(string instituteCode) => $"{TantargyiAtlagRegionPrefix}{instituteCode}";
 | 
			
		||||
 | 
			
		||||
        public TantargyiAtlagListResponse GetTantargyiAtlag(int userId, string instituteCode, string tantargyUid, int? oktatasiNevelesiFeladatId)
 | 
			
		||||
        {
 | 
			
		||||
            string key = GetCacheKey(TantargyiAtlagCacheKeyPrefix, userId, instituteCode, tantargyUid, oktatasiNevelesiFeladatId);
 | 
			
		||||
            string region = GetTantargyiAtlagRegionKey(instituteCode);
 | 
			
		||||
 | 
			
		||||
            return (TantargyiAtlagListResponse)GetByKeyAndRegion(key, region);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void AddOrUpdateTantargyiAtlag(int userId, string instituteCode, string tantargyUid, int? oktatasiNevelesiFeladatId, TantargyiAtlagListResponse value)
 | 
			
		||||
        {
 | 
			
		||||
            AddOrUpdateWithRegion(GetCacheKey(TantargyiAtlagCacheKeyPrefix, userId, instituteCode, tantargyUid, oktatasiNevelesiFeladatId), GetTantargyiAtlagRegionKey(instituteCode), value, k => value);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void RemoveTantargyiAtlag(int userId, string instituteCode, string tantargyUid, int? oktatasiNevelesiFeladatId)
 | 
			
		||||
        {
 | 
			
		||||
            string key = GetCacheKey(TantargyiAtlagCacheKeyPrefix, userId, instituteCode, tantargyUid, oktatasiNevelesiFeladatId);
 | 
			
		||||
 | 
			
		||||
            RemoveFromRegion(key, GetTantargyiAtlagRegionKey(instituteCode));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Értékelés adásánál szükséges készítenünk egy fake kulcsot ami majd adott késleltetés lejáratakor ki fogja ütni az adott tantárgyi atlag cache hogy frissülni tudjon.
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="userId">user id-ja</param>
 | 
			
		||||
        /// <param name="instituteCode">intézmény</param>
 | 
			
		||||
        /// <param name="tantargyUid">tantargy uid</param>
 | 
			
		||||
        /// <param name="oktatasiNevelesiFeladatId">oktatasnevelesfeladat id</param>
 | 
			
		||||
        /// <param name="timeSpan">késleltetés időtartama</param>
 | 
			
		||||
        public void SetTantargyiAtlagExpiration(int userId, string instituteCode, string tantargyUid, int? oktatasiNevelesiFeladatId, TimeSpan timeSpan)
 | 
			
		||||
        {
 | 
			
		||||
            string cacheKey = GetCacheKey(TantargyiAtlagCacheKeyPrefix, userId, instituteCode, tantargyUid, oktatasiNevelesiFeladatId);
 | 
			
		||||
            var triggerKey = $"{TantargyiAtlagTriggerPrefix}{DateTime.Now.Add(timeSpan):yyyyMMddHHmmss}:{GetTantargyiAtlagRegionKey(instituteCode)}:{cacheKey}";
 | 
			
		||||
            AddWithAbsoluteExpiration(triggerKey, timeSpan, string.Empty);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        ///Adott intézményben az összes tantárgyi átlaghoz tartozó cache-t törlése
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public void ClearTantargyiAtlagRegion(string instituteCode)
 | 
			
		||||
        {
 | 
			
		||||
            ClearRegion(GetTantargyiAtlagRegionKey(instituteCode));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								Kreta.BusinessLogic/Caching/TanuloAdatlapCache.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								Kreta.BusinessLogic/Caching/TanuloAdatlapCache.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Kreta.Core;
 | 
			
		||||
using Kreta.Ellenorzo.Domain.VN.Felhasznalo.Tanulo;
 | 
			
		||||
using Kreta.Framework.Caching;
 | 
			
		||||
 | 
			
		||||
namespace Kreta.BusinessLogic.Caching
 | 
			
		||||
{
 | 
			
		||||
    public class TanuloAdatlapCache : GenericCache<object>
 | 
			
		||||
    {
 | 
			
		||||
        private static readonly string TanuloAdatlapCacheKeyPrefix = $"{Constants.Cache.CacheKeyPrefix}TanuloAdatlapCache_";
 | 
			
		||||
 | 
			
		||||
        public TanuloAdatlapCache(Framework.Caching.CacheManager cacheManager) : base(cacheManager, nameof(TanuloAdatlapCache))
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public TanuloAdatResponse GetOrAddTanuloAdatlap(int userId, string instituteCode, Func<TanuloAdatResponse> value)
 | 
			
		||||
        {
 | 
			
		||||
            return GetOrAddValue(TanuloAdatlapCacheKeyPrefix, userId, instituteCode, value);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void RemoveTanuloAdatlap(int userId, string instituteCode)
 | 
			
		||||
        {
 | 
			
		||||
            RemoveValue(TanuloAdatlapCacheKeyPrefix, userId, instituteCode);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected string GetCacheKey(string keyPrefix, int userId, string instituteCode)
 | 
			
		||||
        {
 | 
			
		||||
            return $"{keyPrefix}{instituteCode}.{userId}";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected T GetOrAddValue<T>(string cacheKeyPrefix, int userId, string instituteCode, Func<T> valueFactory) where T : class
 | 
			
		||||
        {
 | 
			
		||||
            string key = GetCacheKey(cacheKeyPrefix, userId, instituteCode);
 | 
			
		||||
            object value = GetOrAdd(key, _ => valueFactory());
 | 
			
		||||
 | 
			
		||||
            return (T)value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected void RemoveValue(string cacheKeyPrefix, int userId, string instituteCode)
 | 
			
		||||
        {
 | 
			
		||||
            string key = GetCacheKey(cacheKeyPrefix, userId, instituteCode);
 | 
			
		||||
            Remove(key);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user