delphimvcframework/sources/MVCFramework.Cache.pas

247 lines
5.5 KiB
ObjectPascal
Raw Normal View History

// *************************************************************************** }
//
// Delphi MVC Framework
//
2018-01-29 17:30:53 +01:00
// Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ***************************************************************************
unit MVCFramework.Cache;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, System.Rtti,
System.SyncObjs, MVCFramework.Commons, System.DateUtils;
const
DEFAULT_CACHE_DURATION = OneMinute * 10;
type
TMVCCacheItem = class
private
FCriticalSection: TCriticalSection;
FValue: TValue;
2018-01-29 17:30:53 +01:00
FTimeStamp: TDateTime;
function GetValue: TValue;
procedure SetValue(const Value: TValue);
public
constructor Create;
destructor Destroy; override;
property Value: TValue read GetValue write SetValue;
2018-01-29 17:30:53 +01:00
property TimeStamp: TDateTime read FTimeStamp;
end;
TMVCCache = class sealed
private
FStorage: TObjectDictionary<string, TMVCCacheItem>;
2017-10-09 16:17:12 +02:00
FMREW: TMultiReadExclusiveWriteSynchronizer;
public
constructor Create;
destructor Destroy; override;
2018-01-29 17:30:53 +01:00
function SetValue(const AName: string; const AValue: TValue): TMVCCacheItem;
function Contains(const AName: string; out AValue: TValue): Boolean;
2018-01-29 17:30:53 +01:00
function ContainsItem(const AName: string; out AItem: TMVCCacheItem): Boolean;
function GetValue(const AName: string): TValue;
2017-10-09 16:17:12 +02:00
function ExecOnItemWithWriteLock(const AName: string; const AAction: TProc<TValue>): Boolean;
end;
TMVCCacheSingleton = class
private
class var Lock: TObject;
class var SInstance: TMVCCache;
protected
class function GetInstance: TMVCCache; static;
public
class property Instance: TMVCCache read GetInstance;
class constructor Create;
class destructor Destroy;
end;
implementation
uses
System.Math;
{ TCache }
2018-01-29 17:30:53 +01:00
function TMVCCache.SetValue(const AName: string; const AValue: TValue): TMVCCacheItem;
var
lValue: TValue;
2018-01-29 17:30:53 +01:00
lCacheItem: TMVCCacheItem;
begin
lValue := AValue;
2017-10-09 16:17:12 +02:00
FMREW.DoWithWriteLock(
procedure
var
lItem: TMVCCacheItem;
begin
if FStorage.TryGetValue(AName, lItem) then
begin
lItem.Value := lValue;
end
else
begin
2018-01-29 17:30:53 +01:00
lCacheItem := TMVCCacheItem.Create;
try
2018-01-29 17:30:53 +01:00
lCacheItem.Value := lValue;
FStorage.Add(AName, lCacheItem);
except
2018-01-29 17:30:53 +01:00
lCacheItem.Free;
raise;
end;
end;
end);
2018-01-29 17:30:53 +01:00
Result := lCacheItem;
end;
2017-10-09 16:17:12 +02:00
function TMVCCache.ExecOnItemWithWriteLock(const AName: string; const AAction: TProc<TValue>): Boolean;
var
lItem: TMVCCacheItem;
begin
Result := False;
2017-10-09 16:17:12 +02:00
FMREW.BeginWrite;
try
if FStorage.TryGetValue(AName, lItem) then
begin
Result := True;
AAction(lItem.Value);
end;
finally
2017-10-09 16:17:12 +02:00
FMREW.EndWrite;
end;
end;
function TMVCCache.Contains(const AName: string; out AValue: TValue): Boolean;
var
2018-01-29 17:30:53 +01:00
lItem: TMVCCacheItem;
begin
Result := ContainsItem(AName, lItem);
if Result then
AValue := lItem.Value;
end;
function TMVCCache.ContainsItem(const AName: string;
out AItem: TMVCCacheItem): Boolean;
var
lItem: TMVCCacheItem;
lRes: Boolean;
begin
2017-10-09 16:17:12 +02:00
FMREW.DoWithReadLock(
procedure
begin
2018-01-29 17:30:53 +01:00
lRes := FStorage.TryGetValue(AName, lItem);
end);
Result := lRes;
if Result then
2018-01-29 17:30:53 +01:00
AItem := lItem;
end;
constructor TMVCCache.Create;
begin
inherited Create;
FStorage := TObjectDictionary<string, TMVCCacheItem>.Create([doOwnsValues]);
2017-10-09 16:17:12 +02:00
FMREW := TMultiReadExclusiveWriteSynchronizer.Create;
end;
destructor TMVCCache.Destroy;
begin
2017-10-09 16:17:12 +02:00
FMREW.Free;
FStorage.Free;
inherited;
end;
function TMVCCache.GetValue(const AName: string): TValue;
var
lItem: TMVCCacheItem;
lResult: TValue;
begin
Result := TValue.Empty;
2017-10-09 16:17:12 +02:00
FMREW.DoWithReadLock(
procedure
begin
if FStorage.TryGetValue(AName, lItem) then
begin
lResult := lItem.Value;
end;
end);
Result := lResult;
end;
{ TMVCFrameworkCacheItem }
constructor TMVCCacheItem.Create;
begin
inherited Create;
FCriticalSection := TCriticalSection.Create;
FValue := TValue.Empty;
2018-01-29 17:30:53 +01:00
FTimeStamp := 0;
end;
destructor TMVCCacheItem.Destroy;
begin
FCriticalSection.Free;
inherited;
end;
function TMVCCacheItem.GetValue: TValue;
begin
Result := FValue;
end;
procedure TMVCCacheItem.SetValue(const Value: TValue);
begin
FValue := Value;
2018-01-29 17:30:53 +01:00
FTimeStamp := Now;
end;
{ TMVCCacheSingleton }
class constructor TMVCCacheSingleton.Create;
begin
SInstance := nil;
Lock := TObject.Create;
end;
class destructor TMVCCacheSingleton.Destroy;
begin
FreeAndNil(SInstance);
FreeAndNil(Lock);
end;
class function TMVCCacheSingleton.GetInstance: TMVCCache;
begin
if not Assigned(SInstance) then
begin
TMonitor.Enter(Lock);
try
if not Assigned(SInstance) then // doublecheck here
begin
SInstance := TMVCCache.Create;
end;
finally
TMonitor.Exit(Lock);
end;
end;
Result := SInstance;
end;
end.