/// <summary> /// Provides cached translations between UniqueIds and USI values. /// </summary> /// <param name="cacheProvider">The cache where the database-specific maps (dictionaries) are stored, expiring after 4 hours of inactivity.</param> /// <param name="edFiOdsInstanceIdentificationProvider">Identifies the ODS instance for the current call.</param> /// <param name="uniqueIdToUsiValueMapper">A component that maps between USI and UniqueId values.</param> /// <param name="personIdentifiersProvider">A component that retrieves all Person identifiers.</param> /// <param name="slidingExpiration">Indicates how long the cache values will remain in memory after being used before all the cached values are removed.</param> /// <param name="absoluteExpirationPeriod">Indicates the maximum time that the cache values will remain in memory before being refreshed.</param> /// <param name="synchronousInitialization">Indicates whether the cache should wait until all the Person identifiers are loaded before responding, or if using the value mappers initially to avoid an initial delay is preferable.</param> public PersonUniqueIdToUsiCache( ICacheProvider cacheProvider, IEdFiOdsInstanceIdentificationProvider edFiOdsInstanceIdentificationProvider, IUniqueIdToUsiValueMapper uniqueIdToUsiValueMapper, IPersonIdentifiersProvider personIdentifiersProvider, TimeSpan slidingExpiration, TimeSpan absoluteExpirationPeriod, bool synchronousInitialization) { _cacheProvider = cacheProvider; _edFiOdsInstanceIdentificationProvider = edFiOdsInstanceIdentificationProvider; _uniqueIdToUsiValueMapper = uniqueIdToUsiValueMapper; _personIdentifiersProvider = personIdentifiersProvider; _synchronousInitialization = synchronousInitialization; if (slidingExpiration < TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(slidingExpiration), "TimeSpan cannot be a negative value."); } if (absoluteExpirationPeriod < TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(absoluteExpirationPeriod), "TimeSpan cannot be a negative value."); } // Use sliding expiration value, if both are set. if (slidingExpiration > TimeSpan.Zero && absoluteExpirationPeriod > TimeSpan.Zero) { absoluteExpirationPeriod = TimeSpan.Zero; } _slidingExpiration = slidingExpiration; _absoluteExpirationPeriod = absoluteExpirationPeriod; }
protected override void Arrange() { _edFiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); _suppliedId = Guid.NewGuid(); _idValueMapper = Stub <IUniqueIdToIdValueMapper>(); A.CallTo(() => _idValueMapper.GetUniqueId(A <string> .That.IsEqualTo(Staff), A <Guid> .That.IsEqualTo(_suppliedId))) .Returns( new PersonIdentifiersValueMap { Id = _suppliedId, UniqueId = "ABC123" }); SetupCaching(); void SetupCaching() { _memoryCacheProvider = new MemoryCacheProvider { MemoryCache = new MemoryCache(IsolatedForUnitTest) }; _idCache = new PersonUniqueIdToIdCache( _memoryCacheProvider, _edFiOdsInstanceIdentificationProvider, _idValueMapper); } }
protected override void Arrange() { _edFiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>(); A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._)) .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>) new PersonIdentifiersValueMap[0])); _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>(); A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._)) .Returns(new PersonIdentifiersValueMap()); _idValueMapper = Stub <IUniqueIdToIdValueMapper>(); A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._)) .Returns(new PersonIdentifiersValueMap()); SetupCaching(); void SetupCaching() { _memoryCacheProvider = new MemoryCacheProvider { MemoryCache = new MemoryCache(IsolatedForUnitTest) }; _usiCache = new PersonUniqueIdToUsiCache( _memoryCacheProvider, _edFiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider, TimeSpan.Zero, TimeSpan.Zero, synchronousInitialization: true); } }
protected override void Arrange() { edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); edOrgValueMapper = Stub <IEducationOrganizationIdentifiersValueMapper>(); educationOrganizationIdentifiersProvider = Stub <IEducationOrganizationCacheDataProvider>(); educationOrganizationIdentifiersProvider.Stub(x => x.GetAllEducationOrganizationIdentifiers()) .IgnoreArguments() .Return( Task.Run( () => (IEnumerable <EducationOrganizationIdentifiers>) new EducationOrganizationIdentifiers[0])); // edorg value mapper suppliedEdOrgValueMap = new EducationOrganizationIdentifiers( educationOrganizationId: 123456, educationOrganizationType: "District", stateEducationAgencyId: 1, educationServiceCenterId: 330950, localEducationAgencyId: 123456); edOrgValueMapper.Stub(x => x.GetEducationOrganizationIdentifiers(default(int))) .IgnoreArguments() .Return(suppliedEdOrgValueMap); }
protected override void Arrange() { _edFiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>(); A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._)) .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>) new PersonIdentifiersValueMap[0])); _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>(); A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._)).Returns(new PersonIdentifiersValueMap()); _idValueMapper = Stub <IUniqueIdToIdValueMapper>(); A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._)) .Returns(new PersonIdentifiersValueMap()); var memorycacheoption = A.Fake <IOptions <MemoryCacheOptions> >(); MemoryCache memoryCache = new MemoryCache(memorycacheoption); _memoryCacheProvider = new MemoryCacheProvider(memoryCache); _usiCache = new PersonUniqueIdToUsiCache( _memoryCacheProvider, _edFiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider, TimeSpan.Zero, TimeSpan.Zero, synchronousInitialization: true); }
public DescriptorsCache( IDescriptorLookupProvider descriptorLookupProvider, ICacheProvider cacheProvider, IEdFiOdsInstanceIdentificationProvider edFiOdsInstanceIdentificationProvider) { _descriptorLookupProvider = descriptorLookupProvider; _cacheProvider = cacheProvider; _edFiOdsInstanceIdentificationProvider = edFiOdsInstanceIdentificationProvider; }
public PersonUniqueIdToIdCache( ICacheProvider cacheProvider, IEdFiOdsInstanceIdentificationProvider edFiOdsInstanceIdentificationProvider, IUniqueIdToIdValueMapper uniqueIdToIdValueMapper, int cacheExpirationMinutes = 10) { _cacheProvider = cacheProvider; _edFiOdsInstanceIdentificationProvider = edFiOdsInstanceIdentificationProvider; _uniqueIdToIdValueMapper = uniqueIdToIdValueMapper; _slidingExpiration = new TimeSpan(0, cacheExpirationMinutes, 0); }
/// <summary> /// /// </summary> /// <param name="cacheProvider">The cache where the database-specific maps (dictionaries) are stored, expiring after 4 hours of inactivity.</param> /// <param name="edFiOdsInstanceIdentificationProvider">Identifies the ODS instance for the current call.</param> /// <param name="educationOrganizationIdentifiersValueMapper">A component that maps between USI and UniqueId values.</param> /// <param name="educationOrganizationIdentifiersProvider">A component that retrieves all education organization identifiers.</param> /// <param name="synchronousInitialization">Indicates whether the cache should wait until all the Person identifiers are loaded before responding, or if using the value mappers initially to avoid an initial delay is preferable.</param> public EducationOrganizationCache( ICacheProvider cacheProvider, IEdFiOdsInstanceIdentificationProvider edFiOdsInstanceIdentificationProvider, IEducationOrganizationIdentifiersValueMapper educationOrganizationIdentifiersValueMapper, IEducationOrganizationCacheDataProvider educationOrganizationIdentifiersProvider, bool synchronousInitialization) { _cacheProvider = cacheProvider; _edFiOdsInstanceIdentificationProvider = edFiOdsInstanceIdentificationProvider; _educationOrganizationIdentifiersValueMapper = educationOrganizationIdentifiersValueMapper; _educationOrganizationIdentifiersProvider = educationOrganizationIdentifiersProvider; _synchronousInitialization = synchronousInitialization; }
private IDescriptorsCache MockNHibernateCallsAndInitializeCache() { MockDescriptorCacheDataProvider = A.Fake <IDescriptorLookupProvider>(); A.CallTo(() => MockDescriptorCacheDataProvider.GetAllDescriptorLookups()) .Returns( new Dictionary <string, IList <DescriptorLookup> > { { TestDescriptorName, new List <DescriptorLookup> { TestDescriptorNormal, TestDescriptorCustom, TestDescriptorWithCodeValue } } }); Dictionary <string, IList <DescriptorLookup> > values = new Dictionary <string, IList <DescriptorLookup> > { { TestDescriptorName, new List <DescriptorLookup> { TestDescriptorNormal, TestDescriptorCustom, TestDescriptorWithCodeValue } } }; var memorycacheoption = A.Fake <IOptions <MemoryCacheOptions> >(); MemoryCache memoryCache = new MemoryCache(memorycacheoption); CacheProvider = new MemoryCacheProvider(memoryCache); CacheProvider.Insert(TestDescriptorName, values, DateTime.MaxValue, TimeSpan.FromMinutes(5)); MockEdFiOdsInstanceIdentificationProvider = A.Fake <IEdFiOdsInstanceIdentificationProvider>(); A.CallTo(() => MockEdFiOdsInstanceIdentificationProvider.GetInstanceIdentification()) .Returns(1); descriptionCache = new DescriptorsCache( MockDescriptorCacheDataProvider, CacheProvider, MockEdFiOdsInstanceIdentificationProvider); return(descriptionCache); }
protected override void Arrange() { _edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>(); _suppliedUsiValueMap = new PersonIdentifiersValueMap { UniqueId = Guid.NewGuid() .ToString(), Usi = 10, Id = Guid.NewGuid() // Id is also provided by the USI value mapper }; A.CallTo(() => _usiValueMapper.GetUniqueId(A <string> ._, A <int> ._)) .Returns(_suppliedUsiValueMap); _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>(); A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._)) .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>) new PersonIdentifiersValueMap[0])); SetupCaching(); void SetupCaching() { var memorycacheoption = A.Fake <IOptions <MemoryCacheOptions> >(); MemoryCache memoryCache = new MemoryCache(memorycacheoption); _memoryCacheProvider = new MemoryCacheProvider(memoryCache); _usiCache = new PersonUniqueIdToUsiCache( _memoryCacheProvider, _edfiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider, TimeSpan.Zero, TimeSpan.Zero, synchronousInitialization: true); PersonUniqueIdToUsiCache.GetCache = () => _usiCache; } }
protected override void Arrange() { edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); edOrgValueMapper = Stub <IEducationOrganizationIdentifiersValueMapper>(); educationOrganizationIdentifiersProvider = Stub <IEducationOrganizationCacheDataProvider>(); A.CallTo(() => educationOrganizationIdentifiersProvider.GetAllEducationOrganizationIdentifiers()) .WithAnyArguments() .Returns( Task.Run( () => (IEnumerable <EducationOrganizationIdentifiers>) new EducationOrganizationIdentifiers[0])); edOrgValueMapper = Stub <IEducationOrganizationIdentifiersValueMapper>(); A.CallTo(() => edOrgValueMapper.GetEducationOrganizationIdentifiers(default(int))) .WithAnyArguments() .Returns(EducationOrganizationIdentifiers.CreateLookupInstance(default(int))); }
protected override void Arrange() { edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); edOrgValueMapper = Stub <IEducationOrganizationIdentifiersValueMapper>(); educationOrganizationIdentifiersProvider = Stub <IEducationOrganizationCacheDataProvider>(); // edorg value mapper suppliedEdOrgValueMap = new EducationOrganizationIdentifiers( educationOrganizationId: 123456, educationOrganizationType: "District", stateEducationAgencyId: 1, educationServiceCenterId: 330950, localEducationAgencyId: 123456); A.CallTo(() => edOrgValueMapper.GetEducationOrganizationIdentifiers(default(int))) .WithAnyArguments() .Returns(suppliedEdOrgValueMap); // edorg identifiers provider suppliedEducationOrganizationIdentifiers = new EducationOrganizationIdentifiers( educationOrganizationId: 123456, educationOrganizationType: "District", stateEducationAgencyId: 1, educationServiceCenterId: 330950, localEducationAgencyId: 8675309); A.CallTo(() => educationOrganizationIdentifiersProvider.GetAllEducationOrganizationIdentifiers()) .WithAnyArguments() .Returns( Task.Run( () => { System.Threading.Thread.Sleep(10); return((IEnumerable <EducationOrganizationIdentifiers>) new[] { suppliedEducationOrganizationIdentifiers }); })); }
protected override void Arrange() { edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); edOrgValueMapper = Stub <IEducationOrganizationIdentifiersValueMapper>(); educationOrganizationIdentifiersProvider = Stub <IEducationOrganizationCacheDataProvider>(); educationOrganizationIdentifiersProvider.Stub(x => x.GetAllEducationOrganizationIdentifiers()) .IgnoreArguments() .Return( Task.Run( () => (IEnumerable <EducationOrganizationIdentifiers>) new EducationOrganizationIdentifiers[0])); edOrgValueMapper = mocks.StrictMock <IEducationOrganizationIdentifiersValueMapper>(); SetupResult.For(edOrgValueMapper.GetEducationOrganizationIdentifiers(default(int))) .IgnoreArguments() .Return(EducationOrganizationIdentifiers.CreateLookupInstance(default(int))); }
protected override void Arrange() { _edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); _idValueMapper = Stub <IUniqueIdToIdValueMapper>(); _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>(); _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>(); _suppliedUsiValueMap = new PersonIdentifiersValueMap { UniqueId = Guid.NewGuid() .ToString(), Usi = 10, Id = Guid.NewGuid() // Id is also provided by the USI value mapper }; _suppliedPersonIdentifiers = new List <PersonIdentifiersValueMap> { new PersonIdentifiersValueMap { UniqueId = Guid.NewGuid() .ToString(), Usi = 100, Id = Guid.NewGuid() }, new PersonIdentifiersValueMap { UniqueId = Guid.NewGuid() .ToString(), Usi = 101, Id = Guid.NewGuid() } }; A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._)) .Returns(new PersonIdentifiersValueMap()); A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._)) .Returns(_suppliedUsiValueMap); A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._)) .Returns(Task.Run(() => (IEnumerable <PersonIdentifiersValueMap>)_suppliedPersonIdentifiers)); SetupCaching(); void SetupCaching() { _memoryCacheProvider = new MemoryCacheProvider { MemoryCache = new MemoryCache(IsolatedForUnitTest) }; _idCache = new PersonUniqueIdToIdCache( _memoryCacheProvider, _edfiOdsInstanceIdentificationProvider, _idValueMapper); _usiCache = new PersonUniqueIdToUsiCache( _memoryCacheProvider, _edfiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider, TimeSpan.Zero, TimeSpan.Zero, synchronousInitialization: true); PersonUniqueIdToUsiCache.GetCache = () => _usiCache; } }
protected override void Arrange() { _edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); _idValueMapper = Stub <IUniqueIdToIdValueMapper>(); _usiValueMapper = Stub <IUniqueIdToUsiValueMapper>(); _personIdentifiersProvider = Stub <IPersonIdentifiersProvider>(); // usi value mapper _suppliedUsiValueMap = new PersonIdentifiersValueMap { UniqueId = Guid.NewGuid() .ToString(), Usi = 10 }; A.CallTo(() => _usiValueMapper.GetUsi(A <string> ._, A <string> ._)) .Returns(_suppliedUsiValueMap); // id value mapper _suppliedIdValueMap = new PersonIdentifiersValueMap { UniqueId = _suppliedUsiValueMap.UniqueId, // Same uniqueId Id = Guid.NewGuid() }; A.CallTo(() => _idValueMapper.GetId(A <string> ._, A <string> ._)) .Returns(_suppliedIdValueMap); _suppliedPersonIdentifiersValueMap = new PersonIdentifiersValueMap { UniqueId = Guid.NewGuid() .ToString(), Usi = 100 }; A.CallTo(() => _personIdentifiersProvider.GetAllPersonIdentifiers(A <string> ._)) .Returns( Task.Run( () => { //Force slight delay during initialization to mimic real behavior. Thread.Sleep(10); return ((IEnumerable <PersonIdentifiersValueMap>) new[] { _suppliedPersonIdentifiersValueMap }); } )); SetupCaching(); void SetupCaching() { _memoryCacheProvider = new MemoryCacheProvider { MemoryCache = new MemoryCache(IsolatedForUnitTest) }; _idCache = new PersonUniqueIdToIdCache( _memoryCacheProvider, _edfiOdsInstanceIdentificationProvider, _idValueMapper); _usiCache = new PersonUniqueIdToUsiCache( _memoryCacheProvider, _edfiOdsInstanceIdentificationProvider, _usiValueMapper, _personIdentifiersProvider, TimeSpan.Zero, TimeSpan.Zero, synchronousInitialization: false); } }
protected override void Arrange() { edfiOdsInstanceIdentificationProvider = Stub <IEdFiOdsInstanceIdentificationProvider>(); edOrgValueMapper = Stub <IEducationOrganizationIdentifiersValueMapper>(); educationOrganizationIdentifiersProvider = Stub <IEducationOrganizationCacheDataProvider>(); // edorg value mapper suppliedEdOrgValueMap = new EducationOrganizationIdentifiers( educationOrganizationId: 123456, educationOrganizationType: "District", stateEducationAgencyId: 1, educationServiceCenterId: 330950, localEducationAgencyId: 123456); edOrgValueMapper.Stub(x => x.GetEducationOrganizationIdentifiers(default(int))) .IgnoreArguments() .Return(suppliedEdOrgValueMap); // edorg identifiers provider suppliedEducationOrganizationIdentifiers = new EducationOrganizationIdentifiers( educationOrganizationId: 123456, educationOrganizationType: "District", stateEducationAgencyId: 1, educationServiceCenterId: 330950, localEducationAgencyId: 8675309); var memoryCacheProvider = new MemoryCacheProvider { MemoryCache = new MemoryCache("IsolatedForUnitTest") }; var educationOrganizationCache = new EducationOrganizationCache( memoryCacheProvider, edfiOdsInstanceIdentificationProvider, edOrgValueMapper, educationOrganizationIdentifiersProvider, synchronousInitialization: false); var listLock = new object(); for (var i = 0; i < TaskCount; i++) { _tasks.Add( new Task( () => { var educationOrganizationIdentifiers = educationOrganizationCache.GetEducationOrganizationIdentifiers( suppliedEducationOrganizationIdentifiers.EducationOrganizationId); lock (listLock) { actualEducationOrganizationIdentifiers.Add(educationOrganizationIdentifiers); } })); } educationOrganizationIdentifiersProvider.Stub(x => x.GetAllEducationOrganizationIdentifiers()) .IgnoreArguments() .Return( Task.Run( () => { Task.WaitAll(_tasks.ToArray()); return((IEnumerable <EducationOrganizationIdentifiers>) new[] { suppliedEducationOrganizationIdentifiers }); })); }