Skip to content

Commit

Permalink
Fix bug with sqlite getting experation. Finalize API and Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Oct 13, 2018
1 parent 39c83a2 commit 5ece018
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 313 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 James Montemagno
Copyright (c) 2018 James Montemagno

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ async Task<IEnumerable<Monkey>> GetMonkeysAsync()
//Dev handle online/offline scenario
if(!CrossConnectivity.Current.IsConnected)
{
return Barrel.Current.GetObject<IEnumerable<Monkey>>(key: url);
return Barrel.Current.Get<IEnumerable<Monkey>>(key: url);
}

//Dev handles checking if cache is expired
if(!Barrel.Current.IsExpired(key: url))
{
return Barrel.Current.GetObject<IEnumerable<Monkey>>(key: url);
return Barrel.Current.Get<IEnumerable<Monkey>>(key: url);
}


Expand All @@ -79,7 +79,7 @@ async Task<IEnumerable<Monkey>> GetMonkeysAsync()
var monkeys = JsonConvert.DeserializeObject<IEnumerable<Monkey>>(json);

//Saves the cache and pass it a timespan for expiration
Barrel.Current.AddObject(key: url, data: monkeys, expireIn: TimeSpan.FromDays(1));
Barrel.Current.Add(key: url, data: monkeys, expireIn: TimeSpan.FromDays(1));

}
```
Expand All @@ -92,10 +92,10 @@ public async Task<T> GetAsync<T>(string url, int days = 7, bool forceRefresh = f
var json = string.Empty;

if (!CrossConnectivity.Current.IsConnected)
json = Barrel.Current.Get(url);
json = Barrel.Current.Get<string>(url);

if (!forceRefresh && !Barrel.Current.IsExpired(url))
json = Barrel.Current.Get(url);
json = Barrel.Current.Get<string>(url);

try
{
Expand All @@ -104,7 +104,7 @@ public async Task<T> GetAsync<T>(string url, int days = 7, bool forceRefresh = f
json = await client.GetStringAsync(url);
Barrel.Current.Add(url, json, TimeSpan.FromDays(days));
}
return await Task.Run(() => JsonConvert.DeserializeObject<T>(json));
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception ex)
{
Expand Down
79 changes: 42 additions & 37 deletions src/MonkeyCache.FileStore/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ public class Barrel : IBarrel
/// <param name="data">Data object to store</param>
/// <param name="expireIn">Time from UtcNow to expire entry in</param>
/// <param name="eTag">Optional eTag information</param>
public void Add(string key, string data, TimeSpan expireIn, string eTag = null)
void Add(string key, string data, TimeSpan expireIn, string eTag = null)
{
if (data == null)
return;

indexLocker.EnterWriteLock();

try
Expand Down Expand Up @@ -80,13 +77,29 @@ public void Add(string key, string data, TimeSpan expireIn, string eTag = null)
/// <param name="expireIn">Time from UtcNow to expire entry in</param>
/// <param name="eTag">Optional eTag information</param>
/// <param name="jsonSerializationSettings">Custom json serialization settings to use</param>
public void AddObject<T>(string key,
public void Add<T>(string key,
T data,
TimeSpan expireIn,
string eTag = null,
JsonSerializerSettings jsonSerializationSettings = null)
{
var dataJson = JsonConvert.SerializeObject(data, jsonSerializationSettings ?? jsonSettings);

if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

if (data == null)
throw new ArgumentNullException("Data can not be null.", nameof(data));

var dataJson = string.Empty;

if (Utils.IsString(data))
{
dataJson = data as string;
}
else
{
dataJson = JsonConvert.SerializeObject(data, jsonSerializationSettings ?? jsonSettings);
}

Add(key, dataJson, expireIn, eTag);
}
Expand All @@ -104,6 +117,9 @@ public void Empty(params string[] key)
{
foreach (var k in key)
{
if (string.IsNullOrWhiteSpace(k))
continue;

File.Delete(Path.Combine(baseDirectory.Value, Hash(k)));
index.Remove(k);
}
Expand Down Expand Up @@ -181,6 +197,9 @@ public void EmptyExpired()
/// <returns>If the key exists</returns>
public bool Exists(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

var exists = false;

indexLocker.EnterReadLock();
Expand All @@ -197,41 +216,18 @@ public bool Exists(string key)
return exists;
}

/// <summary>
/// Gets the string entry for the specified key.
/// </summary>
/// <param name="key">Unique identifier for the entry to get</param>
/// <returns>The string that was stored if found, else null</returns>
public string Get(string key)
{
string result = null;

indexLocker.EnterReadLock();

try
{
var hash = Hash(key);
var path = Path.Combine(baseDirectory.Value, hash);

if (index.ContainsKey(key) && File.Exists(path))
result = File.ReadAllText(path);
}
finally
{
indexLocker.ExitReadLock();
}

return result;
}

/// <summary>
/// Gets the data entry for the specified key.
/// </summary>
/// <param name="key">Unique identifier for the entry to get</param>
/// <param name="jsonSerializationSettings">Custom json serialization settings to use</param>
/// <returns>The data object that was stored if found, else default(T)</returns>
public T GetObject<T>(string key, JsonSerializerSettings jsonSerializationSettings = null)
public T Get<T>(string key, JsonSerializerSettings jsonSerializationSettings = null)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

var result = default(T);

indexLocker.EnterReadLock();
Expand All @@ -244,6 +240,12 @@ public T GetObject<T>(string key, JsonSerializerSettings jsonSerializationSettin
if (index.ContainsKey(key) && File.Exists(path))
{
var contents = File.ReadAllText(path);
if (Utils.IsString(result))
{
object final = contents;
return (T)final;
}

result = JsonConvert.DeserializeObject<T>(contents, jsonSerializationSettings ?? jsonSettings);
}
}
Expand All @@ -262,8 +264,8 @@ public T GetObject<T>(string key, JsonSerializerSettings jsonSerializationSettin
/// <returns>The expiration date if the key is found, else null</returns>
public DateTime? GetExpiration(string key)
{
if (key == null)
return null;
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

DateTime? date = null;

Expand All @@ -289,8 +291,8 @@ public T GetObject<T>(string key, JsonSerializerSettings jsonSerializationSettin
/// <returns>The ETag if the key is found, else null</returns>
public string GetETag(string key)
{
if (key == null)
return null;
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

string etag = null;

Expand All @@ -316,6 +318,9 @@ public string GetETag(string key)
/// <returns>If the expiration data has been met</returns>
public bool IsExpired(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

var expired = true;

indexLocker.EnterReadLock();
Expand Down
68 changes: 48 additions & 20 deletions src/MonkeyCache.LiteDB/Barrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public class Barrel : IBarrel
/// <returns>If the key exists</returns>
public bool Exists(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

var ent = col.FindById(key);

return ent != null;
Expand All @@ -76,6 +79,9 @@ public bool Exists(string key)
/// <returns>If the expiration data has been met</returns>
public bool IsExpired(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

var ent = col.FindById(key);

if (ent == null)
Expand All @@ -94,38 +100,38 @@ public bool IsExpired(string key)
/// <param name="key">Unique identifier for the entry to get</param>
/// <param name="jsonSerializationSettings">Custom json serialization settings to use</param>
/// <returns>The data object that was stored if found, else default(T)</returns>
public T GetObject<T>(string key, JsonSerializerSettings jsonSerializationSettings = null)
public T Get<T>(string key, JsonSerializerSettings jsonSerializationSettings = null)
{
var ent = col.FindById(key);

if (ent == null)
return default(T);
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

return JsonConvert.DeserializeObject<T>(ent.Contents, jsonSerializationSettings ?? jsonSettings);
}
var result = default(T);

/// <summary>
/// Gets the string entry for the specified key.
/// </summary>
/// <param name="key">Unique identifier for the entry to get</param>
/// <returns>The string that was stored if found, else null</returns>
public string Get(string key)
{
var ent = col.FindById(key);

if (ent == null)
return null;
return result;

if (Utils.IsString(result))
{
object final = ent.Contents;
return (T)final;
}

return ent.Contents;
return JsonConvert.DeserializeObject<T>(ent.Contents, jsonSerializationSettings ?? jsonSettings);
}


/// <summary>
/// Gets the ETag for the specified key.
/// </summary>
/// <param name="key">Unique identifier for entry to get</param>
/// <returns>The ETag if the key is found, else null</returns>
public string GetETag(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

var ent = col.FindById(key);

if (ent == null)
Expand All @@ -141,6 +147,9 @@ public string GetETag(string key)
/// <returns>The expiration date if the key is found, else null</returns>
public DateTime? GetExpiration(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

var ent = col.FindById(key);

if (ent == null)
Expand All @@ -161,7 +170,7 @@ public string GetETag(string key)
/// <param name="data">Data string to store</param>
/// <param name="expireIn">Time from UtcNow to expire entry in</param>
/// <param name="eTag">Optional eTag information</param>
public void Add(string key, string data, TimeSpan expireIn, string eTag = null)
void Add(string key, string data, TimeSpan expireIn, string eTag = null)
{
if (data == null)
return;
Expand All @@ -186,12 +195,26 @@ public void Add(string key, string data, TimeSpan expireIn, string eTag = null)
/// <param name="expireIn">Time from UtcNow to expire entry in</param>
/// <param name="eTag">Optional eTag information</param>
/// <param name="jsonSerializationSettings">Custom json serialization settings to use</param>
public void AddObject<T>(string key, T data, TimeSpan expireIn, string eTag = null, JsonSerializerSettings jsonSerializationSettings = null)
public void Add<T>(string key, T data, TimeSpan expireIn, string eTag = null, JsonSerializerSettings jsonSerializationSettings = null)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException("Key can not be null or empty.", nameof(key));

if (data == null)
return;
throw new ArgumentNullException("Data can not be null.", nameof(data));

Add(key, JsonConvert.SerializeObject(data, jsonSerializationSettings ?? jsonSettings), expireIn, eTag);
var dataJson = string.Empty;

if (Utils.IsString(data))
{
dataJson = data as string;
}
else
{
dataJson = JsonConvert.SerializeObject(data, jsonSerializationSettings ?? jsonSettings);
}

Add(key, dataJson, expireIn, eTag);
}

#endregion
Expand All @@ -217,7 +240,12 @@ public void AddObject<T>(string key, T data, TimeSpan expireIn, string eTag = nu
public void Empty(params string[] key)
{
foreach (var k in key)
{
if (string.IsNullOrWhiteSpace(k))
continue;

col.Delete(k);
}
}
#endregion
}
Expand Down
Loading

0 comments on commit 5ece018

Please sign in to comment.