Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
fslpy
Commits
0de46cca
Commit
0de46cca
authored
5 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Cache has an lru option
parent
60a468c7
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/cache.py
+21
-3
21 additions, 3 deletions
fsl/utils/cache.py
with
21 additions
and
3 deletions
fsl/utils/cache.py
+
21
−
3
View file @
0de46cca
...
@@ -42,14 +42,20 @@ class Cache(object):
...
@@ -42,14 +42,20 @@ class Cache(object):
raised.
raised.
"""
"""
def
__init__
(
self
,
maxsize
=
100
):
def
__init__
(
self
,
maxsize
=
100
,
lru
=
False
):
"""
Create a ``Cache``.
"""
Create a ``Cache``.
:arg maxsize: Maximum number of items allowed in the ``Cache`` before
:arg maxsize: Maximum number of items allowed in the ``Cache`` before
it starts dropping old items
it starts dropping old items
:arg lru: (least recently used) If ``False`` (the default), items
are dropped according to their insertion time. Otherwise,
items are dropped according to their most recent access
time.
"""
"""
self
.
__cache
=
collections
.
OrderedDict
()
self
.
__cache
=
collections
.
OrderedDict
()
self
.
__maxsize
=
maxsize
self
.
__maxsize
=
maxsize
self
.
__lru
=
lru
def
put
(
self
,
key
,
value
,
expiry
=
0
):
def
put
(
self
,
key
,
value
,
expiry
=
0
):
...
@@ -94,14 +100,26 @@ class Cache(object):
...
@@ -94,14 +100,26 @@ class Cache(object):
else
:
else
:
entry
=
self
.
__cache
[
key
]
entry
=
self
.
__cache
[
key
]
# Check to see if the entry
# has expired
now
=
time
.
time
()
if
entry
.
expiry
>
0
:
if
entry
.
expiry
>
0
:
if
time
.
time
()
-
entry
.
storetime
>
entry
.
expiry
:
if
now
-
entry
.
storetime
>
entry
.
expiry
:
self
.
__cache
.
pop
(
key
)
self
.
__cache
.
pop
(
key
)
if
defaultSpecified
:
return
default
if
defaultSpecified
:
return
default
else
:
raise
Expired
(
key
)
else
:
raise
Expired
(
key
)
# If we are an lru cache, update
# this entry's expiry, and update
# its order in the cache dict
if
self
.
__lru
:
entry
.
storetime
=
now
self
.
__cache
.
pop
(
key
)
self
.
__cache
[
key
]
=
entry
return
entry
.
value
return
entry
.
value
...
@@ -134,7 +152,7 @@ class Cache(object):
...
@@ -134,7 +152,7 @@ class Cache(object):
- ``True`` if a default argument was specified, ``False``
- ``True`` if a default argument was specified, ``False``
otherwise.
otherwise.
- The specif
e
id default value, or ``None`` if it wasn
'
t
- The specifi
e
d default value, or ``None`` if it wasn
'
t
specified.
specified.
"""
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment