花园宝宝战队 ----- 一阶段代码注释成果 #10
|
|
@ -53,6 +53,18 @@ class DatasetCache:
|
|||
|
||||
def __init__(self, session_id, size=0, spilling=False, hostname=None, port=None, num_connections=None,
|
||||
prefetch_size=None):
|
||||
'''
|
||||
参数:
|
||||
session_id (整数):缓存会话ID。
|
||||
size (整数):缓存大小,默认为0。
|
||||
spilling (布尔值):是否进行溢出处理,默认为False。
|
||||
hostname (字符串):缓存服务器的hostname,默认为None。
|
||||
port (整数):缓存服务器的端口号,默认为None。
|
||||
num_connections (整数):缓存服务器的连接数,默认为None。
|
||||
prefetch_size (整数):预取大小,默认为None。
|
||||
'''
|
||||
|
||||
# 检查参数类型和范围。
|
||||
check_pos_uint32(session_id, "session_id")
|
||||
type_check(size, (int,), "size")
|
||||
if size != 0:
|
||||
|
|
@ -76,24 +88,43 @@ class DatasetCache:
|
|||
self.port = port
|
||||
self.prefetch_size = prefetch_size
|
||||
self.num_connections = num_connections
|
||||
# 创建一个CacheClient对象,用于与缓存服务器进行通信。
|
||||
self.cache_client = CacheClient(session_id, size, spilling, hostname, port, num_connections, prefetch_size)
|
||||
|
||||
def get_stat(self):
|
||||
"""Get the statistics from a cache."""
|
||||
'''
|
||||
这个方法用于获取cache缓存的统计信息,如缓存中数据集的数量、已缓存的数据量等。
|
||||
'''
|
||||
return self.cache_client.GetStat()
|
||||
|
||||
def __deepcopy__(self, memodict):
|
||||
'''
|
||||
这个函数是一个深拷贝函数,用于在内存中深拷贝对象。当对DatasetCache对象进行深拷贝时,会同时复制其所有属性。
|
||||
'''
|
||||
# 如果id(self)在memodict字典中,函数直接返回memodict[id(self)],表示已经存在缓存结果,直接返回。
|
||||
if id(self) in memodict:
|
||||
return memodict[id(self)]
|
||||
cls = self.__class__
|
||||
# 如果不存在id(self)在memodict字典中,那么创建一个新的类实例new_cache,这个实例的类名是cls。
|
||||
new_cache = cls.__new__(cls)
|
||||
# 将self对象的属性复制到new_cache中
|
||||
memodict[id(self)] = new_cache
|
||||
# 将self的session_id复制到新的类中
|
||||
new_cache.session_id = copy.deepcopy(self.session_id, memodict)
|
||||
# 将self的spilling复制到新的类中
|
||||
new_cache.spilling = copy.deepcopy(self.spilling, memodict)
|
||||
# 将self的size复制到新的类中
|
||||
new_cache.size = copy.deepcopy(self.size, memodict)
|
||||
# 将self的hostname复制到新的类中
|
||||
new_cache.hostname = copy.deepcopy(self.hostname, memodict)
|
||||
# 将self的port复制到新的类中
|
||||
new_cache.port = copy.deepcopy(self.port, memodict)
|
||||
# 将self的prefetch_size复制到新的类中
|
||||
new_cache.prefetch_size = copy.deepcopy(self.prefetch_size, memodict)
|
||||
# 将self的num_connections复制到新的类中
|
||||
new_cache.num_connections = copy.deepcopy(self.num_connections, memodict)
|
||||
# 将self的cache_client复制到新的类中
|
||||
new_cache.cache_client = self.cache_client
|
||||
return new_cache
|
||||
# 这样,在后续遇到相同self对象时,可以直接从字典中获取缓存结果,而不需要重新计算耗时函数调用。
|
||||
# 返回新的类
|
||||
return new_cache
|
||||
Loading…
Reference in New Issue