Zope Services ============= .. include:: includes/zope2_notice.rst Some Zope objects are *service* objects. *Service* objects provide various kinds of support to your "domain-specific" content, logic, and presentation objects. They help solve fundamental problems that many others have experienced when writing applications in Zope. Access Rule Services -------------------- *Access Rules* make it possible to cause an action to happen any time a user "traverses" a Folder in your Zope site. When a user's browser submits a request for a URL to Zope which has a Folder's name in it, the Folder is "looked up" by Zope during object publishing. That action (the lookup) is called *traversal*. Access Rules are arbitrary bits of code which effect the environment in some way during Folder traversal. They are easiest to explain by way of an example. .. note::: The Access Service section needs an explanation of how to suppress an access rule. For the baffled among us, you can set an environmental variable 'SUPPRESS_ACCESSRULE' ( I add a line in my 'start' script to do this ) or include '_SUPPRESS_ACCESSRULE' to the URL at a point AFTER the folder/container in question. SITEROOT works the same way, just replace ACCESSRULE with SITEROOT in the above explanation. In your Zope site, create a Folder named "accessrule_test". Inside the accessrule_test folder, create a Script (Python) object named 'access_rule' with two parameters: 'container' and 'request'. Give the 'access_rule' Script (Python) the following body:: useragent = request.get('HTTP_USER_AGENT', '') if useragent.find('Windows') != -1: request.set('OS', 'Windows') elif useragent.find('Linux') != -1: request.set('OS', 'Linux') else: request.set('OS', 'Non-Windows, Non-Linux') This Script causes the traversal of the accessrule_test folder to cause a new variable named 'OS' to be entered into the REQUEST, which has a value of 'Windows', 'Linux', or 'Non-Windows, Non-Linux' depending on the user's browser. Save the 'access_rule' script and revisit the accessrule_test folder's *Contents* view. Choose *Set Access Rule* from the add list. In the 'Rule Id' form field, type 'access_rule'. Then click *Set Rule*. A confirmation screen appears claiming that "'access_rule' is now the Access Rule for this object". Click "OK". Notice that the icon for the 'access_rule' Script (Python) has changed, denoting that it is now the access rule for this Folder. Create a page template named 'test' in the accessrule_test folder with the following text::
request detailsSave the 'test' page template and click its "View" tab. You will see a representation of all the variables that exist in the REQUEST. Note that in the **other** category, there is now a variable named "OS" with (depending on your browser platform) either 'Windows', 'Linux' or 'Non-Linux, Non-Windows'). Revisit the accessrule_test folder and again select *Set Access Rule* from the add list. Click the *No Access Rule* button. A confirmation screen will be displayed stating that the object now has no Access Rule. Visit the 'test' script you created previously and click its *View* tab. You will notice that there is now no "OS" variable listed in the request because we've turned off the Access Rule capability for 'access_rule'. Temporary Storage Services -------------------------- Temporary Folders are Zope folders that are used for storing objects temporarily. Temporary Folders acts almost exactly like a regular Folder with two significant differences: 1. Everything contained in a Temporary Folder disappears when you restart Zope. (A Temporary Folder's contents are stored in RAM). 2. You cannot undo actions taken to objects stored a Temporary Folder. By default there is a Temporary Folder in your root folder named *temp_folder*. You may notice that there is an object entitled, "Session Data Container" within *temp_folder*. This is an object used by Zope's default sessioning system configuration. See the "Using Sessions" section later in this chapter for more information about sessions. Temporary folders store their contents in RAM rather than in the Zope database. This makes them appropriate for storing small objects that receive lots of writes, such as session data. However, it's a bad idea use temporary folders to store large objects because your computer can potentially run out of RAM as a result. Caching Services ---------------- A *cache* is a temporary place to store information that you access frequently. The reason for using a cache is speed. Any kind of dynamic content, like a a Script (Python), must be evaluated each time it is called. For simple pages or quick scripts, this is usually not a problem. For very complex scripts that do a lot of computation or call remote servers, accessing that page or script could take more than a trivial amount of time. Scripts can get this complex, especially if you use lots of looping (such as the Python 'for' loop) or if you call lots of scripts, that in turn call lots of scripts, and so on. Computations that take a lot of time are said to be *expensive*. A cache can add a lot of speed to your site by calling an expensive page or script once and storing the result of that call so that it can be reused. The very first person to call that page will get the usual "slow" response time, but then once the value of the computation is stored in the cache, all subsequent users to call that page will see a very quick response time because they are getting the *cached copy* of the result and not actually going through the same expensive computation the first user went through. To give you an idea of how caches can improve your site speed, imagine that you are creating *www.zopezoo.org*, and that the very first page of your site is very complex. Let's suppose this page has complex headers, footers, queries several different database tables, and calls several special scripts that parse the results of the database queries in complex ways. Every time a user comes to *www.zopezoo.org*, Zope must render this very complex page. For the purposes of demonstration, let's suppose this complex page takes one-half of a second, or 500 milliseconds, to compute. Given that it takes a half of a second to render this fictional complex main page, your machine can only really serve 120 hits per minute. In reality, this number would probably be even lower than that, because Zope has to do other things in addition to just serving up this main page. Now, imagine that you set this page up to be cached. Since none of the expensive computation needs to be done to show the cached copy of the page, many more users could see the main page. If it takes, for example, 10 milliseconds to show a cached page, then this page is being served *50 times faster* to your website visitors. The actual performance of the cache and Zope depends a lot on your computer and your application, but this example gives you an idea of how caching can speed up your website quite a bit. There are some disadvantages to caching however: Cache lifetime If pages are cached for a long time, they may not reflect the most current information on your site. If you have information that changes very quickly, caching may hide the new information from your users because the cached copy contains the old information. How long a result remains cached is called the *cache lifetime* of the information. Personal information Many web pages may be personalized for one particular user. Obviously, caching this information and showing it to another user would be bad due to privacy concerns, and because the other user would not be getting information about *them*, they'd be getting it about someone else. For this reason, caching is often never used for personalized information. Zope allows you to get around these problems by setting up a *cache policy*. The cache policy allows you to control how content gets cached. Cache policies are controlled by *Cache Manager* objects. Adding a Cache Manager ~~~~~~~~~~~~~~~~~~~~~~ Cache managers can be added just like any other Zope object. Currently Zope comes with two kinds of cache managers: HTTP Accelerated Cache Manager An HTTP Accelerated Cache Manager allows you to control an HTTP cache server that is external to Zope, for example, `Squid
Yesterday it rained.
Now, click on the *Weather* page template and click on its *Cache* view. This view lets you associate this page with a cache manager. If you pull down the select box at the top of the view, you'll see the cache manager you created in the previous section, *CacheManager*. Select this as the cache manager for *Weather*. Now, whenever anyone visits the *Weather* page, they will get the cached copy instead. For a page as trivial as our *Weather* example, this is not much of a benefit. But imagine for a moment that *Weather* contained some database queries. For example::
Yesterday's weather was
The current temperature is