-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of the changes for session management persistence to t…
…he database #102 Added all database schema changes and new functions for the above. Created function and schema constants for the database calls and integrated them into the DAL layer.
- Loading branch information
Chris Morris
committed
Jan 12, 2021
1 parent
0b7246b
commit 5b3ba22
Showing
34 changed files
with
939 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
create or replace function caching.date_diff | ||
( | ||
_units character varying, | ||
_start_t timestamp with time zone, | ||
_end_t timestamp with time zone | ||
) | ||
returns integer | ||
as $$ | ||
declare _diff_interval interval; | ||
declare _diff integer = 0; | ||
declare _years_diff integer = 0; | ||
begin | ||
if(_units in ('yy', 'yyyy', 'year', 'mm', 'm', 'month')) then | ||
_years_diff = date_part('year', _end_t) - datepart('year', _start_t); | ||
|
||
if(_units in ('yy', 'yyyy', 'year')) then | ||
return _years_diff; | ||
else | ||
return _years_diff * 12 + (date_part('month', _end_t) - date_part('month', _start_t)); | ||
end if; | ||
end if; | ||
|
||
_diff_interval = _end_t - _start_t; | ||
_diff = _diff + date_part('day', _diff_interval); | ||
|
||
if(_units in ('wk', 'ww', 'week')) then | ||
_diff = _diff / 7; | ||
return _diff; | ||
end if; | ||
|
||
if(_units in ('dd', 'd', 'day')) then | ||
return _diff; | ||
end if; | ||
|
||
_diff = _diff * 24 + date_part('hour', _diff_interval); | ||
|
||
if(_units in ('hh', 'hour')) then | ||
return _diff; | ||
end if; | ||
|
||
_diff = _diff * 60 + date_part('minute', _diff_interval); | ||
|
||
if(_units in ('mi', 'n', 'minute')) then | ||
return _diff; | ||
end if; | ||
|
||
_diff = _diff * 60 + date_part('second', _diff_interval); | ||
|
||
return _diff; | ||
end; | ||
$$ language plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
create or replace function caching.delete_cache_item | ||
( | ||
_dist_cache_id text | ||
) | ||
returns void | ||
as $$ | ||
begin | ||
delete from | ||
caching.dist_cache | ||
where | ||
Id = _dist_cache_id; | ||
end; | ||
$$ language plpgsql; |
13 changes: 13 additions & 0 deletions
13
database/functions/R__caching.delete_expired_cache_items.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
create or replace function caching.delete_expired_cache_items | ||
( | ||
_utc_now timestamp with time zone | ||
) | ||
returns void | ||
as $$ | ||
begin | ||
delete from | ||
caching.dist_cache | ||
where | ||
_utc_now > ExpiresAtTime; | ||
end; | ||
$$ language plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
create or replace function caching.get_cache_item | ||
( | ||
_dist_cache_id text, | ||
_utc_now timestamp with time zone | ||
) | ||
returns table(Id text, Value bytea, ExpiresAtTime timestamp with time zone, SlidingExpirationInSeconds double precision, AbsoluteExpiration timestamp with time zone) | ||
as $$ | ||
begin | ||
return query | ||
select | ||
dc.Id, | ||
dc.Value, | ||
dc.ExpiresAtTime, | ||
dc.SlidingExpirationInSeconds, | ||
dc.AbsoluteExpiration | ||
from | ||
caching.dist_cache dc | ||
where | ||
dc.Id = _dist_cache_id | ||
and _utc_now <= dc.ExpiresAtTime; | ||
end; | ||
$$ language plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
create or replace function caching.set_cache | ||
( | ||
_dist_cache_id text, | ||
_dist_cache_value bytea, | ||
_dist_cache_sliding_expiration_seconds double precision, | ||
_dist_cache_absolute_expiration timestamp with time zone, | ||
_utc_now timestamp with time zone | ||
) | ||
returns void | ||
as $$ | ||
declare _expires_at_time timestamp(6) with time zone; | ||
declare _row_count integer; | ||
begin | ||
case | ||
when (_dist_cache_sliding_expiration_seconds is null) | ||
then _expires_at_time := _dist_cache_absolute_expiration; | ||
else | ||
_expires_at_time := _utc_now + _dist_cache_sliding_expiration_seconds * interval '1 second'; | ||
end case; | ||
|
||
update | ||
caching.dist_cache | ||
set | ||
Value = _dist_cache_value, | ||
ExpiresAtTime = _expires_at_time, | ||
SlidingExpirationInSeconds = _dist_cache_sliding_expiration_seconds, | ||
AbsoluteExpiration = _dist_cache_absolute_expiration | ||
where | ||
Id = _dist_cache_id; | ||
|
||
get diagnostics _row_count := ROW_COUNT; | ||
|
||
if(_row_count = 0) then | ||
insert into caching.dist_cache | ||
( | ||
Id, | ||
Value, | ||
ExpiresAtTime, | ||
SlidingExpirationInSeconds, | ||
AbsoluteExpiration | ||
) | ||
values | ||
( | ||
_dist_cache_id, | ||
_dist_cache_value, | ||
_expires_at_time, | ||
_dist_cache_sliding_expiration_seconds, | ||
_dist_cache_absolute_expiration | ||
); | ||
end if; | ||
end; | ||
$$ language plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
create or replace function caching.update_cache_item | ||
( | ||
_dist_cache_id text, | ||
_utc_now timestamp with time zone | ||
) | ||
returns void | ||
as $$ | ||
begin | ||
update | ||
caching.dist_cache | ||
set | ||
ExpiresAtTime = | ||
case when | ||
(select caching.date_diff('seconds', _utc_now, AbsoluteExpiration) <= SlidingExpirationInSeconds) | ||
then AbsoluteExpiration | ||
else _utc_now + SlidingExpirationInSeconds * interval '1 second' | ||
end | ||
where | ||
Id = _dist_cache_id | ||
and _utc_now <= ExpiresAtTime | ||
and SlidingExpirationInSeconds is not null | ||
and (AbsoluteExpiration is null or AbsoluteExpiration <> ExpiresAtTime); | ||
end; | ||
$$ language plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
create schema caching; | ||
|
||
drop table caching.dist_cache; | ||
|
||
create table caching.dist_cache | ||
( | ||
Id text not null, | ||
Value bytea, | ||
ExpiresAtTime timestamp with time zone, | ||
SlidingExpirationInSeconds double precision, | ||
AbsoluteExpiration timestamp with time zone, | ||
|
||
constraint caching_distcache_id_pk primary key (Id) | ||
); | ||
|
||
grant usage on schema caching to app_user; | ||
grant select, insert, update, delete on all tables in schema caching to app_user; | ||
grant select, update on all sequences in schema caching to app_user; | ||
grant execute on all functions in schema caching to app_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.