-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cachemanager.py
148 lines (127 loc) · 5.13 KB
/
_cachemanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
################################################################################
# _cachemanager.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################
# Imports.
import time
import urllib
import zExceptions
dct_op = {'index':'','sitemap':'sitemap','index_print':'print'}
# ------------------------------------------------------------------------------
# _cachemanager.Empty
# ------------------------------------------------------------------------------
class Empty: pass
################################################################################
#
# STATIC CACHE
#
################################################################################
# ------------------------------------------------------------------------------
# _cachemanager._getIdFromUrl
# ------------------------------------------------------------------------------
def _getIdFromUrl(REQUEST):
id = REQUEST['URL']
id = id[:-5]
id = id[id.rfind('/')+1:]
if id.find('_') < 0 and REQUEST.has_key('lang'):
id = '%s_%s'%(id,REQUEST['lang'])
return _getCacheId(id)
# ------------------------------------------------------------------------------
# _cachemanager._getCacheId
# ------------------------------------------------------------------------------
def _getCacheId(id):
return 'cache_%s_html'%id
################################################################################
#
# REQUEST BUFFER
#
################################################################################
# ------------------------------------------------------------------------------
# getReqBuffId:
#
# Gets buffer-id in Http-Request.
#
# @throws Exception
# ------------------------------------------------------------------------------
def getReqBuffId(self, key, REQUEST):
id = '%s#%s.%s'%( '/'.join(self.getPhysicalPath()),key,REQUEST.get('lang','*'))
return id
# ------------------------------------------------------------------------------
# getReqBuff:
#
# Gets buffer from Http-Request.
#
# @throws Exception
# ------------------------------------------------------------------------------
def getReqBuff(self, REQUEST):
buff = REQUEST.get('__buff__',None)
if buff == None:
buff = Empty()
return buff
################################################################################
#
# ReqBuff
#
################################################################################
class ReqBuff:
# --------------------------------------------------------------------------
# fetchReqBuff:
#
# Fetch buffered value from Http-Request.
#
# @throws Exception
# --------------------------------------------------------------------------
def fetchReqBuff(self, key, REQUEST, forced=False):
if REQUEST.get('ZMS_FETCH_REQ_BUFF',True):
url = REQUEST.get('PSEUDOURL',REQUEST.get('URL','/manage'))
url = url[url.rfind('/'):]
if forced or not url.find('/manage') >= 0:
buff = getReqBuff(self,REQUEST)
reqBuffId = getReqBuffId(self,key,REQUEST)
try:
value = getattr(buff,reqBuffId)
return value
except:
raise zExceptions.InternalError('%s not found in ReqBuff!'%reqBuffId)
raise zExceptions.InternalError('ReqBuff is inactive!')
# --------------------------------------------------------------------------
# storeReqBuff:
#
# Returns value and stores it in buffer of Http-Request.
# --------------------------------------------------------------------------
def storeReqBuff(self, key, value, REQUEST):
try:
buff = getReqBuff(self,REQUEST)
reqBuffId = getReqBuffId(self,key,REQUEST)
setattr(buff,reqBuffId,value)
REQUEST.set('__buff__',buff)
except:
pass
return value
# --------------------------------------------------------------------------
# clearReqBuff:
#
# Clears key from buffer of Http-Request.
# --------------------------------------------------------------------------
def clearReqBuff(self, key, REQUEST):
try:
buff = getReqBuff(self,REQUEST)
reqBuffId = getReqBuffId(self,key,REQUEST)
delattr(buff,reqBuffId)
REQUEST.set('__buff__',buff)
except:
pass
################################################################################