Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Apr 12, 2013
2 parents 00b35ec + 12f0f8c commit bbebbc6
Show file tree
Hide file tree
Showing 28 changed files with 7,315 additions and 283 deletions.
42 changes: 17 additions & 25 deletions src/ImageProcessor.Web/Caching/CachedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,46 @@ namespace ImageProcessor.Web.Caching
{
#region Using
using System;

using SQLite;

#endregion

/// <summary>
/// Describes a cached image
/// </summary>
internal sealed class CachedImage
public sealed class CachedImage
{
/// <summary>
/// Initializes a new instance of the <see cref="CachedImage"/> class.
/// Gets or sets the id identifying the cached image.
/// </summary>
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

/// <summary>
/// Gets or sets the key identifying the cached image.
/// </summary>
/// <param name="path">
/// The value of the cached image.
/// </param>
/// <param name="maxAge">
/// The max age of the cached image.
/// </param>
/// <param name="lastWriteTimeUtc">
/// The last write time of the cached image.
/// </param>
/// <param name="expiresTimeUtc">
/// The expires time.
/// </param>
public CachedImage(string path, int maxAge, DateTime lastWriteTimeUtc, DateTime expiresTimeUtc)
{
this.Path = path;
this.MaxAge = maxAge;
this.LastWriteTimeUtc = lastWriteTimeUtc;
this.ExpiresUtc = expiresTimeUtc;
}
[Unique]
public string Key { get; set; }

/// <summary>
/// Gets or sets the value of the cached image.
/// </summary>
internal string Path { get; set; }
public string Path { get; set; }

/// <summary>
/// Gets or sets the maximum age of the cached image in days.
/// </summary>
internal int MaxAge { get; set; }
public int MaxAge { get; set; }

/// <summary>
/// Gets or sets the last write time of the cached image.
/// </summary>
internal DateTime LastWriteTimeUtc { get; set; }
public DateTime LastWriteTimeUtc { get; set; }

/// <summary>
/// Gets or sets when the cached image should expire from the cache.
/// </summary>
internal DateTime ExpiresUtc { get; set; }
public DateTime ExpiresUtc { get; set; }
}
}
10 changes: 9 additions & 1 deletion src/ImageProcessor.Web/Caching/DiskCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,15 @@ internal async Task AddImageToCacheAsync(DateTime lastWriteTimeUtc)
{
string key = Path.GetFileNameWithoutExtension(this.CachedPath);
DateTime expires = DateTime.UtcNow.AddDays(MaxFileCachedDuration).ToUniversalTime();
CachedImage cachedImage = new CachedImage(this.CachedPath, MaxFileCachedDuration, lastWriteTimeUtc, expires);
CachedImage cachedImage = new CachedImage
{
Key = key,
Path = this.CachedPath,
MaxAge = MaxFileCachedDuration,
LastWriteTimeUtc = lastWriteTimeUtc,
ExpiresUtc = expires
};

await PersistantDictionary.Instance.AddAsync(key, cachedImage);
}

Expand Down
20 changes: 12 additions & 8 deletions src/ImageProcessor.Web/Caching/PersistantDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ public async Task<bool> TryRemoveAsync(string key)

// Remove the CachedImage.
CachedImage value = this[key];
this.Remove(key);

await this.SaveCacheAsync(key, value, true);
return true;
if (await this.SaveCacheAsync(key, value, true) > 0)
{
this.Remove(key);
return true;
}

return false;
}

/// <summary>
Expand All @@ -97,7 +101,7 @@ public async Task<bool> TryRemoveAsync(string key)
public async Task<CachedImage> AddAsync(string key, CachedImage cachedImage)
{
// Add the CachedImage.
if (await this.SaveCacheAsync(key, cachedImage, false))
if (await this.SaveCacheAsync(key, cachedImage, false) > 0)
{
this.Add(key, cachedImage);
}
Expand All @@ -121,20 +125,20 @@ public async Task<CachedImage> AddAsync(string key, CachedImage cachedImage)
/// <returns>
/// true, if the dictionary is saved to the file-system; otherwise, false.
/// </returns>
private async Task<bool> SaveCacheAsync(string key, CachedImage cachedImage, bool remove)
private async Task<int> SaveCacheAsync(string key, CachedImage cachedImage, bool remove)
{
try
{
if (remove)
{
return await SQLContext.RemoveImageAsync(key);
return await SQLContext.RemoveImageAsync(cachedImage);
}

return await SQLContext.AddImageAsync(key, cachedImage);
return await SQLContext.AddImageAsync(cachedImage);
}
catch
{
return false;
return 0;
}
}

Expand Down
169 changes: 33 additions & 136 deletions src/ImageProcessor.Web/Caching/SQLContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
namespace ImageProcessor.Web.Caching
{
#region Using

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Threading.Tasks;
using System.Web.Hosting;
using ImageProcessor.Web.Config;
using ImageProcessor.Web.Helpers;

using SQLite;

#endregion

/// <summary>
Expand All @@ -37,7 +40,7 @@ internal sealed class SQLContext
/// <summary>
/// The connection string.
/// </summary>
private static readonly string ConnectionString = string.Format("Data Source={0};Version=3;", IndexLocation);
private static readonly string ConnectionString = IndexLocation;
#endregion

#region Methods
Expand All @@ -47,47 +50,33 @@ internal sealed class SQLContext
/// </summary>
internal static void CreateDatabase()
{
if (!File.Exists(IndexLocation))
try
{
string absolutePath = HostingEnvironment.MapPath(VirtualCachePath);

if (absolutePath != null)
if (!File.Exists(IndexLocation))
{
DirectoryInfo directoryInfo = new DirectoryInfo(absolutePath);
string absolutePath = HostingEnvironment.MapPath(VirtualCachePath);

if (!directoryInfo.Exists)
if (absolutePath != null)
{
// Create the directory.
Directory.CreateDirectory(absolutePath);
}
}

SQLiteConnection.CreateFile(IndexLocation);

using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
connection.Open();
DirectoryInfo directoryInfo = new DirectoryInfo(absolutePath);

using (SQLiteTransaction transaction = connection.BeginTransaction())
{
using (SQLiteCommand command = new SQLiteCommand(connection))
if (!directoryInfo.Exists)
{
command.CommandText = @"CREATE TABLE names
(Key TEXT,
Path TEXT,
MaxAge INTEGER,
LastWriteTimeUtc TEXT,
ExpiresUtc TEXT,
PRIMARY KEY (Key),
UNIQUE (Path));";

command.ExecuteNonQuery();
// Create the directory.
Directory.CreateDirectory(absolutePath);
}
}

transaction.Commit();
using (SQLiteConnection connection = new SQLiteConnection(IndexLocation))
{
connection.CreateTable<CachedImage>();
}
}
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
Expand All @@ -104,25 +93,11 @@ internal static Dictionary<string, CachedImage> GetImages()
{
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
connection.Open();
List<CachedImage> images = connection.Query<CachedImage>("SELECT * FROM CachedImage");

using (SQLiteCommand command = new SQLiteCommand(connection))
foreach (CachedImage cachedImage in images)
{
command.CommandText = "SELECT * FROM names;";

SQLiteDataReader reader = command.ExecuteReader();

while (reader.Read())
{
string key = reader["Key"].ToString();
CachedImage image = new CachedImage(
reader["Path"].ToString(),
int.Parse(reader["MaxAge"].ToString()),
DateTime.Parse(reader["LastWriteTimeUtc"].ToString()).ToUniversalTime(),
DateTime.Parse(reader["ExpiresUtc"].ToString()).ToUniversalTime());

dictionary.Add(key, image);
}
dictionary.Add(cachedImage.Key, cachedImage);
}
}

Expand All @@ -137,124 +112,46 @@ internal static Dictionary<string, CachedImage> GetImages()
/// <summary>
/// Adds a cached image to the database.
/// </summary>
/// <param name="key">
/// The key for the cached image.
/// </param>
/// <param name="image">
/// The cached image to add.
/// </param>
/// <returns>
/// The true if the addition of the cached image is added; otherwise, false.
/// </returns>
internal static async Task<bool> AddImageAsync(string key, CachedImage image)
{
// Create Action delegate for AddImage.
return await TaskHelpers.Run(() => AddImage(key, image));
}

/// <summary>
/// Removes a cached image from the database.
/// </summary>
/// <param name="key">
/// The key for the cached image.
/// </param>
/// <returns>
/// The true if the addition of the cached image is removed; otherwise, false.
/// </returns>
internal static async Task<bool> RemoveImageAsync(string key)
{
// Create Action delegate for RemoveImage.
return await TaskHelpers.Run(() => RemoveImage(key));
}
#endregion

#region Private
/// <summary>
/// Adds a cached image to the database.
/// </summary>
/// <param name="key">
/// The key for the cached image.
/// </param>
/// <param name="image">
/// The cached image to add.
/// </param>
/// <returns>
/// The true if the addition of the cached image is added; otherwise, false.
/// </returns>
private static bool AddImage(string key, CachedImage image)
internal static async Task<int> AddImageAsync(CachedImage image)
{
try
{
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
connection.Open();

using (SQLiteTransaction transaction = connection.BeginTransaction())
{
using (SQLiteCommand command = new SQLiteCommand(connection))
{
command.CommandText = "INSERT INTO names VALUES(?, ?, ?, ?, ?)";

SQLiteParameter[] parameters = new[]
{
new SQLiteParameter("Key", key),
new SQLiteParameter("Path", image.Path),
new SQLiteParameter("MaxAge", image.MaxAge),
new SQLiteParameter("LastWriteTimeUtc", image.LastWriteTimeUtc),
new SQLiteParameter("ExpiresUtc", image.ExpiresUtc)
};

command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
}
SQLiteAsyncConnection connection = new SQLiteAsyncConnection(ConnectionString);

transaction.Commit();
}
}

return true;
return await connection.InsertAsync(image);
}
catch
{
return false;
return 0;
}
}

/// <summary>
/// Removes a cached image from the database.
/// </summary>
/// <param name="key">
/// <param name="cachedImage">
/// The key for the cached image.
/// </param>
/// <returns>
/// The true if the addition of the cached image is removed; otherwise, false.
/// </returns>
private static bool RemoveImage(string key)
internal static async Task<int> RemoveImageAsync(CachedImage cachedImage)
{
try
{
using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
{
connection.Open();

using (SQLiteTransaction transaction = connection.BeginTransaction())
{
using (SQLiteCommand command = new SQLiteCommand(connection))
{
command.CommandText = "DELETE FROM names WHERE key = @searchParam;";
command.Parameters.Add(new SQLiteParameter("searchParam", key));
command.ExecuteNonQuery();
}

transaction.Commit();
}
}
SQLiteAsyncConnection connection = new SQLiteAsyncConnection(ConnectionString);

return true;
return await connection.DeleteAsync(cachedImage);
}
catch
{
return false;
return 0;
}
}
#endregion
Expand Down
Loading

0 comments on commit bbebbc6

Please sign in to comment.