Should dynamic business objects for a site be stored in the users session or use ASP.Net caching (objects such as orders, profile information etc)?
站点的动态业务对象应该存储在用户会话中或使用ASP。Net缓存(对象如订单、概要信息等)?
I have worked with sites that used sessions to store business objects, but I was wondering...What are the advantages or disadvantages of caching?
我曾与使用会话存储业务对象的网站合作,但我想知道……缓存的优点和缺点是什么?
5 个解决方案
#1
23
If the objects are shareable between user sessions, then use the cache. If the objects are unique to each session -- perhaps because they are governed by permissions -- then store it in the session. The in-process session itself is stored in the cache so the deciding factor really should be the scope of the data.
如果对象在用户会话之间共享,则使用缓存。如果每个会话的对象都是惟一的,可能是因为它们受权限管理,然后将其存储在会话中。进程内会话本身存储在缓存中,因此决定因素实际上应该是数据的范围。
#2
6
Caching is just that -- caching. You can never rely on entries being there, so no assumptions must be made in that respect: be prepared to go straight to the DB (or wherever else) to refetch data.
缓存就是这样——缓存。您永远不能依赖于存在的条目,因此在这方面不需要做任何假设:准备直接到DB(或其他任何地方)重新获取数据。
Session, on the other hand, is more suited towards storing objects, though personally I try to avoid session store in favour of a DB. I usually do that by abstracting away the store behind an opaque ISessionStoreService interface:
另一方面,会话更适合存储对象,尽管我个人尽量避免使用会话存储,而使用DB。我通常通过抽象不透明的ISessionStoreService接口后的存储来实现:
interface ISessionStore
{
T GetEntry<T>(string key);
void SaveEntry<T>(string key, T entry);
}
and then "dependency-injecting" appropriate implementation, be it InmemorySessionStore, DbSessionStore or whatever.
然后“依赖注入”适当的实现,无论是在memorysessionstore、DbSessionStore还是其他什么。
#3
2
The ASP.NET system cache is global to the application where as the session is unique to the current user. If you chose to use the global cache to store objects you would need to create an object identification strategy so you could get the correct objects for your per user basis.
ASP。NET系统缓存对于应用程序是全局的,其中会话对于当前用户是惟一的。如果您选择使用全局缓存来存储对象,则需要创建对象识别策略,这样您就可以为每个用户提供正确的对象。
If you are looking to enhance performance you would be better off replacing the ASP.NET session state with a distributed memory cache such as Microsoft's velocity. Microsoft has posted articles on how to replace session usage to target Velocity. You could also use Memcache or other similar products in a related fashion.
如果您希望提高性能,您最好替换ASP。具有分布式内存缓存的网络会话状态,如Microsoft的velocity。微软已经发布了关于如何将会话使用替换为目标速度的文章。您还可以以相关的方式使用Memcache或其他类似产品。
#4
2
Session objects are suitable for user-only-data, on the other hand, Cache objects are more suitable for data shared for the application.
The key to save in one or the other is to determine if what are you trying to store will be user-only data or it needs to be shared in all the application.
会话对象适合于用户数据,另一方面,缓存对象更适合应用程序共享的数据。保存其中一个或另一个的关键是确定您试图存储的是用户数据还是需要在所有应用程序*享。
Session => A webpage with a step by step interface (an online test for example).
Cache => The info displayed in some kind of weather widget (like the one that google has in its igoogle.com page).
Hope this helps.
Session =>一步一步界面的网页(比如在线测试)。Cache =>在某种天气小部件中显示的信息(比如谷歌在igoogle.com页面中显示的信息)。希望这个有帮助。
#5
1
Although you can store your business object in Cache, but Cache is designed for performance improvement not state management. Imagine you that you have a process of getting 1000 record from database (and it take about 3 seconds) and you will need it for a few minutes. You can store your objects in Cache and set expire date, priority and dependency to it (like SqlDependency or FileDependency), so for next requests you can use Cached data instead of retriving it from database. You can store your object in Session but you can not set dependency for Session by default. Also Cache has a unique behavior that when system needs memory it will release objects from cache depending on its priority. Cache objects are global to Application and shared between all users but Session is not shared and it's usable for each user (Session).
虽然可以将业务对象存储在缓存中,但是缓存的设计目的是提高性能,而不是状态管理。假设您有一个从数据库获得1000个记录的过程(大约需要3秒),您将需要它几分钟。您可以将对象存储在缓存中,并设置过期日期、优先级和对它的依赖关系(如SqlDependency或FileDependency),因此对于下一个请求,您可以使用缓存的数据,而不是从数据库中检索它。您可以将对象存储在会话中,但默认情况下不能为会话设置依赖项。此外,缓存有一个独特的行为,当系统需要内存时,它将根据其优先级从缓存中释放对象。缓存对象对于应用程序是全局的,并且在所有用户之间共享,但是会话不是共享的,并且对每个用户(会话)都是可用的。
#1
23
If the objects are shareable between user sessions, then use the cache. If the objects are unique to each session -- perhaps because they are governed by permissions -- then store it in the session. The in-process session itself is stored in the cache so the deciding factor really should be the scope of the data.
如果对象在用户会话之间共享,则使用缓存。如果每个会话的对象都是惟一的,可能是因为它们受权限管理,然后将其存储在会话中。进程内会话本身存储在缓存中,因此决定因素实际上应该是数据的范围。
#2
6
Caching is just that -- caching. You can never rely on entries being there, so no assumptions must be made in that respect: be prepared to go straight to the DB (or wherever else) to refetch data.
缓存就是这样——缓存。您永远不能依赖于存在的条目,因此在这方面不需要做任何假设:准备直接到DB(或其他任何地方)重新获取数据。
Session, on the other hand, is more suited towards storing objects, though personally I try to avoid session store in favour of a DB. I usually do that by abstracting away the store behind an opaque ISessionStoreService interface:
另一方面,会话更适合存储对象,尽管我个人尽量避免使用会话存储,而使用DB。我通常通过抽象不透明的ISessionStoreService接口后的存储来实现:
interface ISessionStore
{
T GetEntry<T>(string key);
void SaveEntry<T>(string key, T entry);
}
and then "dependency-injecting" appropriate implementation, be it InmemorySessionStore, DbSessionStore or whatever.
然后“依赖注入”适当的实现,无论是在memorysessionstore、DbSessionStore还是其他什么。
#3
2
The ASP.NET system cache is global to the application where as the session is unique to the current user. If you chose to use the global cache to store objects you would need to create an object identification strategy so you could get the correct objects for your per user basis.
ASP。NET系统缓存对于应用程序是全局的,其中会话对于当前用户是惟一的。如果您选择使用全局缓存来存储对象,则需要创建对象识别策略,这样您就可以为每个用户提供正确的对象。
If you are looking to enhance performance you would be better off replacing the ASP.NET session state with a distributed memory cache such as Microsoft's velocity. Microsoft has posted articles on how to replace session usage to target Velocity. You could also use Memcache or other similar products in a related fashion.
如果您希望提高性能,您最好替换ASP。具有分布式内存缓存的网络会话状态,如Microsoft的velocity。微软已经发布了关于如何将会话使用替换为目标速度的文章。您还可以以相关的方式使用Memcache或其他类似产品。
#4
2
Session objects are suitable for user-only-data, on the other hand, Cache objects are more suitable for data shared for the application.
The key to save in one or the other is to determine if what are you trying to store will be user-only data or it needs to be shared in all the application.
会话对象适合于用户数据,另一方面,缓存对象更适合应用程序共享的数据。保存其中一个或另一个的关键是确定您试图存储的是用户数据还是需要在所有应用程序*享。
Session => A webpage with a step by step interface (an online test for example).
Cache => The info displayed in some kind of weather widget (like the one that google has in its igoogle.com page).
Hope this helps.
Session =>一步一步界面的网页(比如在线测试)。Cache =>在某种天气小部件中显示的信息(比如谷歌在igoogle.com页面中显示的信息)。希望这个有帮助。
#5
1
Although you can store your business object in Cache, but Cache is designed for performance improvement not state management. Imagine you that you have a process of getting 1000 record from database (and it take about 3 seconds) and you will need it for a few minutes. You can store your objects in Cache and set expire date, priority and dependency to it (like SqlDependency or FileDependency), so for next requests you can use Cached data instead of retriving it from database. You can store your object in Session but you can not set dependency for Session by default. Also Cache has a unique behavior that when system needs memory it will release objects from cache depending on its priority. Cache objects are global to Application and shared between all users but Session is not shared and it's usable for each user (Session).
虽然可以将业务对象存储在缓存中,但是缓存的设计目的是提高性能,而不是状态管理。假设您有一个从数据库获得1000个记录的过程(大约需要3秒),您将需要它几分钟。您可以将对象存储在缓存中,并设置过期日期、优先级和对它的依赖关系(如SqlDependency或FileDependency),因此对于下一个请求,您可以使用缓存的数据,而不是从数据库中检索它。您可以将对象存储在会话中,但默认情况下不能为会话设置依赖项。此外,缓存有一个独特的行为,当系统需要内存时,它将根据其优先级从缓存中释放对象。缓存对象对于应用程序是全局的,并且在所有用户之间共享,但是会话不是共享的,并且对每个用户(会话)都是可用的。