Skip to content

Commit

Permalink
fix cache issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Sep 7, 2023
1 parent 615d442 commit c125de2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/FluentCommand/DataCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,17 +659,16 @@ private string CacheKey<T>(bool supportCache)
if (_slidingExpiration == null && _absoluteExpiration == null)
return null;


var connectionString = Command.Connection?.ConnectionString;
var commandText = Command.CommandText;
var commandType = Command.CommandType;
var type = typeof(T);
var typeName = typeof(T).FullName;

var hashCode = HashCode.Seed
.Combine(connectionString)
.Combine(commandType)
.Combine(commandText)
.Combine(type);
.Combine(typeName);

foreach (IDbDataParameter parameter in Command.Parameters)
{
Expand All @@ -682,7 +681,7 @@ private string CacheKey<T>(bool supportCache)
.Combine(parameter.DbType);
}

return $"global:data:{hashCode}";
return $"fluent:data:query:{hashCode:X}";
}

private (bool Success, T Value) GetCache<T>(string key)
Expand Down
94 changes: 78 additions & 16 deletions src/FluentCommand/Internal/HashCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace FluentCommand.Internal;
/// <remarks>
/// Implements the Jon Skeet suggested implementation of GetHashCode().
/// </remarks>
public readonly struct HashCode
public readonly struct HashCode : IFormattable, IEquatable<HashCode>
{
/// <summary>
/// The prime multiplier used to combine hash codes.
Expand Down Expand Up @@ -108,16 +108,71 @@ public HashCode CombineAll<TValue>(IEnumerable<TValue> values)
return new HashCode(current);
}


/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
return _hashCode;
}
public override int GetHashCode() => _hashCode;


/// <summary>
/// Converts to string.
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// </returns>
public override string ToString() => _hashCode.ToString();

/// <summary>
/// Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>
/// The string representation of the value of this instance as specified by provider.
/// </returns>
public string ToString(IFormatProvider provider) => _hashCode.ToString(provider);

/// <summary>
/// Converts the numeric value of this instance to its equivalent string representation using the specified format.
/// </summary>
/// <param name="format">A standard or custom numeric format string.</param>
/// <returns>
/// The string representation of the value of this instance as specified by format.
/// </returns>
public string ToString(string format) => _hashCode.ToString(format);

/// <summary>
/// Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
/// </summary>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>
/// The string representation of the value of this instance as specified by format and provider.
/// </returns>
public string ToString(string format, IFormatProvider provider) => _hashCode.ToString(format, provider);


/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj) => obj is HashCode code && Equals(code);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.
/// </returns>
public bool Equals(HashCode other) => _hashCode == other._hashCode;


/// <summary>
/// Performs an implicit conversion from <see cref="HashCode"/> to <see cref="int"/>.
Expand All @@ -126,24 +181,31 @@ public override int GetHashCode()
/// <returns>
/// The result of the conversion.
/// </returns>
public static implicit operator int(HashCode hashCode)
{
return hashCode._hashCode;
}
public static implicit operator int(HashCode hashCode) => hashCode._hashCode;

/// <summary>
/// Converts to string.
/// Compares two values to determine equality.
/// </summary>
/// <param name="left">The value to compare with right.</param>
/// <param name="right">The value to compare with left.</param>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// true if left is equal to right; otherwise, false.
/// </returns>
public override string ToString()
{
return _hashCode.ToString();
}
public static bool operator ==(HashCode left, HashCode right) => left.Equals(right);

/// <summary>
/// Compares two values to determine inequality.
/// </summary>
/// <param name="left">The value to compare with right.</param>
/// <param name="right">The value to compare with left.</param>
/// <returns>
/// true if left is not equal to right; otherwise, false.
/// </returns>
public static bool operator !=(HashCode left, HashCode right) => !(left == right);


/// <summary>
/// Deterministic string hash
/// Deterministic string hash function
/// </summary>
/// <param name="text">The text to hash.</param>
/// <returns>A 32-bit signed integer hash code.</returns>
Expand Down

0 comments on commit c125de2

Please sign in to comment.