Hình 8-6 trình bày các khái niệm bạn cần để làm việc với nhà nước ứng dụng và bộ nhớ đệm. Để bắt đầu, một ứng dụng được tạo thành từ tất cả các trang, code, và các file khác đang nằm dưới một thư mục duy nhất trong một máy chủ web IIS.
Lần đầu tiên người dùng yêu cầu một trang nằm trong thư mục của một ứng dụng, khởi ASP.NET các ứng dụng. Trong quá trình đó, ASP.NET tạo ra một đối tượng ứng dụng từ lớp HttpApplication, một đối tượng nhà nước áp dụng từ lớp HttpApplicationState, và một đối tượng bộ nhớ cache từ các lớp Cache. Bạn có thể sử dụng các đối tượng nhà nước ứng dụng hoặc các đối tượng bộ nhớ cache để lưu trữ dữ liệu trong bộ nhớ máy chủ có thể được truy cập bởi bất kỳ trang nào đó là một phần của ứng dụng.
Các đối tượng này tồn tại cho đến khi ứng dụng kết thúc, mà thông thường không xảy ra cho đến khi IIS tắt. Tuy nhiên, các ứng dụng cũng được khởi động lại mỗi khi bạn xây dựng lại các ứng dụng hoặc chỉnh sửa file web.config của ứng dụng.
Vì vậy mà ở cấp ứng dụng lưu trữ, bạn nên sử dụng? Nói chung, bạn sẽ muốn sử dụng các đối tượng bộ nhớ cache, vì nó linh hoạt hơn. Điều này là bởi vì bạn có thể thiết lập ngày hết hạn cho các mặt hàng được lưu trữ trong bộ nhớ cache, và bởi vì các máy chủ được cho phép để quét dọn bộ nhớ cache khi bộ nhớ đang ở mức thấp. Bởi vì điều này, bạn có thể lưu trữ chỉ là về bất cứ điều gì bạn muốn trong bộ nhớ cache mà không lo lắng về ảnh hưởng tiêu cực đến các máy chủ, miễn là bạn nhớ rằng các sản phẩm có thể không có được khi bạn trở lại cho họ. Vì vậy, luôn luôn, kiểm tra giá trị null trong mã của bạn trước khi cố gắng sử dụng một cái gì đó mà bạn đã lấy từ bộ nhớ cache.
Caching thường được sử dụng để lưu trữ dữ liệu ứng dụng cụ thể mà thay đổi thường xuyên. Ví dụ, bạn có thể sử dụng bộ nhớ đệm để lưu trữ các điều khoản chiết khấu và thuế suất đối với một hệ thống đặt hàng, hoặc một danh sách của 50 tiểu bang. Mặc dù bạn có thể lấy các loại thông tin này từ một cơ sở dữ liệu mỗi khi cần thiết, nó có thể được lấy ra nhanh chóng hơn từ bộ nhớ cache. Kết quả là, sử dụng dữ liệu được lưu trữ có thể cải thiện hiệu suất ứng dụng của bạn.
Ngược lại, các mục được lưu trữ trong ứng dụng của nhà nước ở trong bộ nhớ cho đến khi họ được cụ thể loại bỏ hoặc cho đến khi ứng dụng kết thúc. Điều này có thể có tác động tiêu cực đến hiệu suất của máy chủ nếu rất nhiều dữ liệu được lưu trữ. Vì lý do này, nó là tốt nhất để sử dụng nhà nước áp dụng cho các hạng mục nhỏ dữ liệu, chẳng hạn như theo dõi của người dùng đăng nhập vào một ứng dụng cung cấp một phòng chat hoặc diễn đàn
đang được dịch, vui lòng đợi..