Skip to content

Commit

Permalink
Merge pull request #647 from zhyifei/fix_profile_cache_notsync
Browse files Browse the repository at this point in the history
Fix the issue of user profile cache being out of sync with the database.
  • Loading branch information
neozhu committed Apr 5, 2024
2 parents 7fac9d6 + cf400bf commit 13dd227
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface IIdentityService : IService
Task<ApplicationUserDto> GetApplicationUserDto(string userName, CancellationToken cancellation = default);
string GetUserName(string userId);
Task<List<ApplicationUserDto>?> GetUsers(string? tenantId, CancellationToken cancellation = default);
void RemoveApplicationUserCache(string userName);
}
12 changes: 10 additions & 2 deletions src/Infrastructure/Services/Identity/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public async Task UpdateLiveStatus(string userId, bool isLive, CancellationToken

public async Task<ApplicationUserDto> GetApplicationUserDto(string userName, CancellationToken cancellation = default)
{
var key = $"GetApplicationUserDto:{userName}";
var key = GetApplicationUserCacheKey(userName);
var result = await _cache.GetOrAddAsync(key,
async () => await _userManager.Users.Where(x => x.UserName == userName).Include(x => x.UserRoles)
.ThenInclude(x => x.Role).ProjectTo<ApplicationUserDto>(_mapper.ConfigurationProvider)
.FirstOrDefaultAsync(cancellation) ?? new ApplicationUserDto() { UserName= "Anonymous" }, Options);
.FirstOrDefaultAsync(cancellation) ?? new ApplicationUserDto() { UserName = "Anonymous" }, Options);
return result;
}

Expand All @@ -130,4 +130,12 @@ public async Task<ApplicationUserDto> GetApplicationUserDto(string userName, Can
var result = await _cache.GetOrAddAsync(key, () => getUsersByTenantId(tenantId, cancellation), Options);
return result;
}

public void RemoveApplicationUserCache(string userName)
{
_cache.Remove(GetApplicationUserCacheKey(userName));
}

private string GetApplicationUserCacheKey(string userName) => $"GetApplicationUserDto:{userName}";

}
7 changes: 7 additions & 0 deletions src/Server.UI/Fluxor/Effects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ public async Task HandleFetchDataAction(FetchUserDtoAction action, IDispatcher d
if (result is not null)
dispatcher.Dispatch(new FetchUserDtoResultAction(result));
}

[EffectMethod]
public Task HandleUserProfileChangedAction(UserProfileUpdateAction action, IDispatcher dispatcher)
{
_identityService.RemoveApplicationUserCache(action.UserProfile.UserName);
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -139,7 +139,7 @@
<value>通知</value>
</data>
<data name="Profile" xml:space="preserve">
<value>轮廓</value>
<value>用户资料</value>
</data>
<data name="Search (Cmd + K)" xml:space="preserve">
<value>搜索(Cmd + K)</value>
Expand Down
128 changes: 62 additions & 66 deletions src/Server.UI/Resources/Pages/Identity/Users/Profile.zh-CN.resx
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root" xmlns="">
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
Expand Down Expand Up @@ -112,14 +112,10 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Change Password" xml:space="preserve">
<value>更改密码</value>
Expand Down Expand Up @@ -155,7 +151,7 @@
<value>电话号码</value>
</data>
<data name="Profile" xml:space="preserve">
<value>轮廓</value>
<value>用户资料</value>
</data>
<data name="Save Changes" xml:space="preserve">
<value>保存更改</value>
Expand Down
Loading

0 comments on commit 13dd227

Please sign in to comment.