Ulasan pilihan binari yang boleh didapati25 comments
Option binaire belgique forum
This is a lot more expensive, from a processing-overhead perspective, than your standard read-a-file-off-the-filesystem server arrangement. For convenience, Django offers different levels of cache granularity: You can cache the output of specific views, you can cache only the pieces that are difficult to produce, or you can cache your entire site.
The Cache Framework design philosophy explains a few of the design decisions of the framework. The cache system requires a small amount of setup. Namely, you have to tell it where your cached data should live — whether in a database, on the filesystem or directly in memory.
The fastest, most efficient type of cache supported natively by Django, Memcached is an entirely memory-based cache server, originally developed to handle high loads at LiveJournal.
It is used by sites such as Facebook and Wikipedia to reduce database access and dramatically increase site performance. Memcached runs as a daemon and is allotted a specified amount of RAM. All it does is provide a fast interface for adding, retrieving and deleting data in the cache. There are several Python Memcached bindings available; the two most common are python-memcached and pylibmc. In this example, Memcached is running on localhost When using the pylibmc binding, do not include the unix: One excellent feature of Memcached is its ability to share a cache over multiple servers.
This means you can run Memcached daemons on multiple machines, and the program will treat the group of machines as a single cache, without the need to duplicate cache values on each machine. In this example, the cache is shared over Memcached instances running on IP address In the following example, the cache is shared over Memcached instances running on the IP addresses A final point about Memcached is that memory-based caching has a disadvantage: Django can store its cached data in your database.
If you are using multiple database caches, createcachetable creates one table for each cache. It will only create missing tables. To print the SQL that would be run, rather than run it, use the createcachetable --dry-run option. The file-based backend serializes and stores each cache value as a separate file. The directory path should be absolute — that is, it should start at the root of your filesystem. Make sure the directory pointed-to by this setting exists and is readable and writable by the system user under which your Web server runs.
This is the default cache if another is not specified in your settings file. This cache is per-process see below and thread-safe. If you only have one locmem cache, you can omit the LOCATION ; however, if you have more than one local memory cache, you will need to assign a name to at least one of them in order to keep them separate.
Note that each process will have its own private cache instance, which means no cross-process caching is possible. While Django includes support for a number of cache backends out-of-the-box, sometimes you might want to use a customized cache backend. Each cache backend can be given additional arguments to control caching behavior. Valid arguments are as follows:. The default timeout, in seconds, to use for the cache. This argument defaults to seconds 5 minutes.
Any options that should be passed to the cache backend. The list of valid options will vary with each backend, and cache backends backed by a third-party library will pass their options directly to the underlying cache library. Cache backends that implement their own culling strategy i. The maximum number of entries allowed in the cache before old values are deleted.
This argument defaults to This argument should be an integer and defaults to 3. On some backends database in particular this makes culling much faster at the expense of more cache misses. Memcached backends pass the contents of OPTIONS as keyword arguments to the client constructors, allowing for more advanced control of client behavior. For example usage, see below. A string that will be automatically included prepended by default to all cache keys used by the Django server.
See the cache documentation for more information. The default version number for cache keys generated by the Django server. In this example, a filesystem backend is being configured with a timeout of 60 seconds, and a maximum capacity of items:.
Once the cache is set up, the simplest way to use caching is to cache your entire site. Responses to requests for the same URL with different query parameters are considered to be unique pages and are cached separately. In older versions, the Last-Modified header was also set.
See Middleware for more on middleware. If a view sets its own cache expiry time i. Using the decorators in django. See the using other headers section for more on these decorators.
This allows you to easily cache multilingual sites without having to create the cache key yourself. A more granular way to use the caching framework is by caching the output of individual views. The per-view cache, like the per-site cache, is keyed off of the URL. But once a particular URL e. By default, the default cache will be used, but you can specify any cache you want:.
You can also override the cache prefix on a per-view basis. It can be used like this:. This approach couples your view to the cache system, which is not ideal for several reasons. For instance, you might want to reuse the view functions on another, cache-less site, or you might want to distribute the views to people who might want to use them without being cached.
The solution to these problems is to specify the per-view cache in the URLconf rather than next to the view functions themselves. Doing so is easy: It takes at least two arguments: The fragment is cached forever if timeout is None. The name will be taken as is, do not use a variable. Sometimes you might want to cache multiple copies of a fragment depending on some dynamic data that appears inside the fragment.
For example, you might want a separate cached copy of the sidebar used in the previous example for every user of your site. For the cache template tag you could use one of the translation-specific variables available in templates to achieve the same result:. The cache timeout can be a template variable, as long as the template variable resolves to an integer value. This feature is useful in avoiding repetition in templates. You can set the timeout in a variable, in one place, and just reuse that value.
If no such cache exists, it will fall back to using the default cache. You may select an alternate cache backend to use with the using keyword argument, which must be the last argument to the tag.
This function can be useful for invalidating or overwriting a cached item, for example:. Perhaps, for instance, your site includes a view whose results depend on several expensive queries, the results of which change at different intervals. For cases like this, Django exposes a simple, low-level cache API. You can use this API to store objects in the cache with any level of granularity you like.
You can cache any Python object that can be pickled safely: Most common Python objects can be pickled; refer to the Python documentation for more information about pickling. Repeated requests for the same alias in the same thread will return the same object. If the named key does not exist, InvalidCacheBackendError will be raised. To provide thread-safety, a different instance of the cache backend will be returned for each thread.
As a shortcut, the default cache is available as django. This object is equivalent to caches['default']. The basic interface is set key, value, timeout and get key:.
The timeout argument is optional and defaults to the timeout argument of the appropriate backend in the CACHES setting explained above. Passing in None for timeout will cache the value forever. It takes the same parameters as set , but it will not attempt to update the cache if the key specified is already present:.
If you need to know whether add stored a value in the cache, you can check the return value. It will return True if the value was stored, False otherwise.
It takes the same parameters as get but the default is set as the new cache value for that key, rather than simply returned:. You can delete keys explicitly with delete. This is an easy way of clearing the cache for a particular object:. Finally, if you want to delete all the keys in the cache, use cache.
Be careful with this; clear will remove everything from the cache, not just the keys set by your application. You can also increment or decrement a key that already exists using the incr or decr methods, respectively.