Skip to content

Commit

Permalink
[fix] 修正内存缓存有效期不能超过25天的BUG。原因是内存缓存内部计算过期时间时,乘法运算没有考虑Int32溢出。close: #153
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Feb 2, 2025
1 parent 61056f5 commit 5e63e44
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion NewLife.Core/Caching/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public void Set<T>(T value, Int32 expire)
if (expire <= 0)
ExpiredTime = Int64.MaxValue;
else
ExpiredTime = now + expire * 1000;
ExpiredTime = now + expire * 1000L;
}

/// <summary>设置数值和过期时间</summary>
Expand Down
21 changes: 21 additions & 0 deletions XUnitTest.Core/Caching/MemoryCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,25 @@ public void GetQueue()
var ex = Assert.Throws<InvalidCastException>(() => mc.GetQueue<String>("queue"));
Assert.StartsWith("Unable to convert the value of [queue]", ex.Message);
}

[Theory]
[InlineData(3)]
[InlineData(24)]
[InlineData(25)]
[InlineData(3650)]
public void LongExpireSet(Int32 days)
{
var mc = new MemoryCache();

mc.Set("name", "Stone", TimeSpan.FromDays(days));

var rs = mc.ContainsKey("name");
Assert.True(rs);

var val = mc.Get<String>("name");
Assert.Equal("Stone", val);

var exp = mc.GetExpire("name");
Assert.Equal(days, exp.Days);
}
}

0 comments on commit 5e63e44

Please sign in to comment.