本文整理汇总了Python中memcache._Host函数的典型用法代码示例。如果您正苦于以下问题:Python _Host函数的具体用法?Python _Host怎么用?Python _Host使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_Host函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _node_stats
def _node_stats(host):
"""
Returns a dict with information about a given memcache instance
"""
host = memcache._Host(host)
host.connect()
host.send_cmd("stats")
stats = {}
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
_, key, value = line
try:
# Convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
stats[key] = value
host.close_socket()
return stats
示例2: get_memcached_stats
def get_memcached_stats(server):
if not memcache_installed:
return {}
host = memcache._Host(server)
host.connect()
host.send_cmd("stats")
stats = {}
while True:
try:
stat, key, value = host.readline().split(None, 2)
except ValueError:
break
try:
# Convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
stats[key] = value
host.close_socket()
try:
stats['hit_rate'] = 100 * stats['get_hits'] / stats['cmd_get']
except ZeroDivisionError:
stats['hit_rate'] = stats['get_hits']
return stats
示例3: add_server
def add_server(self, server):
""" Adds a host at runtime to client
"""
# Uncomment this to protect the Client from adding a server in case
# there's no reliable consistent hash algorithm such as MODULO
"""
if not self.consistent_hash_manager:
raise Exception("The current consistent hash algorithm (\"%s\") is"
" not reliable for adding a new server"
"" % self.hash_algorithm)
"""
# Create a new host entry
server = memcache._Host(
server, self.debug, dead_retry=self.dead_retry,
socket_timeout=self.socket_timeout,
flush_on_reconnect=self.flush_on_reconnect
)
# Add this to our server choices
self.servers.append(server)
"""This for statement will ensure that a server with a bigger weight
will have more copies into the buckets increasing it's probability to
be retrieved.
"""
for i in range(server.weight):
self.buckets.append(server)
# Adds this node to the circle
if self.consistent_hash_manager:
self.consistent_hash_manager.add_node(server)
示例4: _query_memcache_server
def _query_memcache_server(location):
try:
host = memcache._Host(location)
host.connect()
host.send_cmd("stats")
stats = MemcachedStats()
while True:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
value = int(value)
if key == "uptime":
value = timedelta(seconds=value)
elif key == "time":
value = datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
return stats
except Exception:
return None
示例5: view
def view(request):
"""
Draw up a horrible little page shewing current memcache stats. 404s if memcache isn't installed.
"""
try:
import memcache
except ImportError:
raise http.Http404
if not (request.user.is_authenticated() and
request.user.is_staff):
raise http.Http404
# get first memcached URI
m = re.match(
"memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
)
if not m:
raise http.Http404
host = memcache._Host(m.group(1))
host.connect()
host.send_cmd("stats")
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
hit_rate = 0
if stats.cmd_get > 0:
hit_rate = 100 * stats.get_hits / stats.cmd_get
return render_to_response(
'memcached_status.html', dict(
stats=stats,
hit_rate=hit_rate,
time=datetime.datetime.now(), # server time
))
示例6: cache_view
def cache_view(request):
"""
Tries to import memcache, fails out if it can't and raises a 404. Also
raises a 404 in the case of unauthenticated users, or memcache not
being used. Graciously borrowed from:
http://effbot.org/zone/django-memcached-view.htm
"""
try:
import memcache
except ImportError:
raise http.Http404
if not (request.user.is_authenticated() and
request.user.is_staff):
raise http.Http404
# get first memcached URI
m = re.match(
"memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
)
if not m:
raise http.Http404
host = memcache._Host(m.group(1))
host.connect()
host.send_cmd("stats")
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
return render_to_response(
'memcached_status.html', dict(
stats=stats,
hit_rate=100 * stats.get_hits / stats.cmd_get,
time=datetime.datetime.now(), # server time
))
示例7: memcache_showkeys
def memcache_showkeys(request):
"""
Displays all the keys and the size for the memcache server IP entered, takes by default 127.0.0.1:11211.
Requires user login with a staff status set true.
"""
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
if request.method == "POST":
try:
import memcache
except ImportError:
raise http.Http404
connection_string = '127.0.0.1:11211'
host = memcache._Host(connection_string)
host.connect()
#getting all keys and size
keyno_dict = {}
host.send_cmd("stats items")
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
if 'number' in line[1]:
keyno = line[1].split(':')[1]
item_size = line[2]
keyno_dict[keyno] = item_size
key_size_dict = {}
for x,y in keyno_dict.items():
cmd = 'stats cachedump ' + x +' '+ y
host.send_cmd(cmd)
while 1:
temp = host.readline().split(None, 2)
if temp[0] == "END":
break
keyname = temp[1]
keysize = temp[2].split()[0].replace('[','')
try:
key_size_dict[keyname] = int(keysize)
except:
key_size_dict[keyname] = 0
host.close_socket()
import operator
soted_key_value_dict =sorted(key_size_dict.iteritems(), key=operator.itemgetter(1))
return render_to_response('show_keys.html', locals(), context_instance = RequestContext(request) )
else:
return HttpResponseRedirect(reverse(memcache_home))
示例8: memcached_status
def memcached_status(request):
# http://effbot.org/zone/django-memcached-view.htm
try:
import memcache
except ImportError:
raise Http404
#if not (request.user.is_authenticated() and
# request.user.is_staff):
# raise Http404
# get first memcached URI
m = re.match(
"([.\w]+:\d+)", settings.CACHES['default']['LOCATION'])
if not m:
raise Http404
host = memcache._Host(m.group(1))
host.connect()
host.send_cmd("stats")
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
try:
hit_rate = 100 * stats.get_hits / stats.cmd_get
except:
hit_rate = 0
return render_to_response(
'cache/memcache_status.html', dict(
stats=stats,
hit_rate=hit_rate,
time=datetime.datetime.now(), # server time
))
示例9: cache_status
def cache_status(request, ):
from django import http
from django.shortcuts import render_to_response
from django.conf import settings
import datetime, re
try:
import memcache
except ImportError:
raise http.Http404
if not (request.user.is_authenticated() and
request.user.is_staff):
raise http.Http404
# get first memcached URI
m = re.match(
"memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
)
if not m:
raise http.Http404
host = memcache._Host(m.group(1))
host.connect()
host.send_cmd("stats")
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
return render_to_response(
'memcached_status.html', dict(
stats=stats,
hit_rate=100 * stats.get_hits / stats.cmd_get,
time=datetime.datetime.now(), # server time
))
示例10: server_list
def server_list(request):
template = 'status.html'
try:
import memcache
except:
raise Http404
stats = {}
cache_servers = {}
dict_vars = {}
servers = None
# It assumes that you're using a right configuration
try:
servers = settings.CACHES['default']['LOCATION']
except:
return render_to_response(template, dict_vars, RequestContext(request))
for server in servers:
host = memcache._Host(server)
host.connect()
if host == 0:
raise Http404
try:
host.send_cmd("stats")
except:
break
while True:
line = host.readline().split(None)
if line[0] == "END":
break
try:
val = int(line[2])
if line[1] == "uptime":
value = datetime.timedelta(seconds=val)
elif line[1] == "time":
value = datetime.datetime.fromtimestamp(val)
except Exception:
pass
stats[line[1]] = line[2]
stats['hit_rate'] = 0
if int(stats['cmd_get']) != 0:
stats['hit_rate'] = 100 * int(stats['get_hits']) / int(stats['cmd_get'])
stats['right_now'] = datetime.datetime.now()
limit_maxbytes = int(stats['bytes'])
if int(stats['limit_maxbytes']) > 0:
limit_maxbytes = int(stats['limit_maxbytes'])
stats['full'] = (100 * int(stats['bytes']) / limit_maxbytes)
# Add info. for this host to list
cache_servers[server] = stats
# Close connection
host.close_socket()
dict_vars['servers'] = cache_servers
return render_to_response(template, dict_vars, RequestContext(request))
示例11: add_server
def add_server(self, server):
""" Adds a host at runtime to client"""
# Create a new host entry
server = memcache._Host(
server, self.debug, dead_retry=self.dead_retry,
socket_timeout=self.socket_timeout,
flush_on_reconnect=self.flush_on_reconnect
)
# Add this to our server choices
self.servers.append(server)
# Update our buckets
self.buckets.append(server)
self.consistent_hash.add_machine(server,len(self.servers)-1)
示例12: view
def view(request):
try:
import memcache
except ImportError:
raise http.Http404
# get first memcached URI
m = re.match(
"memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
)
if not m:
#raise http.Http404
return HttpResponse("No Memcached Server 2")
host = memcache._Host(m.group(1))
host.connect()
host.send_cmd("stats")
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
if stats.cmd_get == 0:
hit_rate = 'na'
else:
hit_rate = 100 * stats.get_hits / stats.cmd_get
return render_to_response(
'memcached_status.html', dict(
stats=stats,
hit_rate=hit_rate,
time=datetime.datetime.now(), # server time
))
示例13: cache_status
def cache_status(request):
"""
Fetch memcache status and display it.
Taken and modified from: http://effbot.org/zone/django-memcached-view.htm
"""
try:
import memcache
except ImportError:
return direct_to_template(request, 'memcachedstatus/cache_status.html', {'error': _(u'Memcached library not installed')})
if not MEMCACHE:
return direct_to_template(request, 'memcachedstatus/cache_status.html', {'error': _(u'No memcached configured')})
host = memcache._Host(MEMCACHE.group(1))
host.connect()
try:
host.send_cmd("stats")
except AttributeError:
return direct_to_template(request, 'memcachedstatus/cache_status.html', {'error': _(u'Memcached not running')})
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
if stats.cmd_get != 0:
context = {'stats': stats,
'hit_rate': 100 * stats.get_hits / stats.cmd_get,}
else:
context = {'stats': stats}
return direct_to_template(request, 'memcachedstatus/cache_status.html', context)
示例14: add_server
def add_server(self, server):
""" Adds a host at runtime to client
"""
# Create a new host entry
server = memcache._Host(
server, self.debug, dead_retry=self.dead_retry,
socket_timeout=self.socket_timeout,
flush_on_reconnect=self.flush_on_reconnect
)
# Add this to our server choices
self.servers.append(server)
self.buckets.append(server)
self.mc_ring.servers.append(server)
self.mc_ring.buckets.append(server)
示例15: memcached_status
def memcached_status(request):
try:
import memcache
except ImportError:
return HttpResponseRedirect("/")
if not (request.user.is_authenticated() and
request.user.is_staff):
return HttpResponseRedirect("/")
# get first memcached URI
m = re.match(
"memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
)
if not m:
return HttpResponseRedirect("/")
host = memcache._Host(m.group(1))
host.connect()
host.send_cmd("stats")
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
return render_to_response(
'webview/memcached_status.html', dict(
stats=stats,
hit_rate=100 * stats.get_hits / stats.cmd_get,
time=datetime.datetime.now(), # server time
), context_instance=RequestContext(request))