Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
D
Django-Redis-Cache
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shared
Django-Redis-Cache
Commits
7d1f8558
Commit
7d1f8558
authored
Sep 05, 2011
by
Sean Bleier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure that caching a float doesn't lose its precision.
parent
709125fa
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
2 deletions
+14
-2
cache.py
redis_cache/cache.py
+5
-2
tests.py
tests/testapp/tests.py
+9
-0
No files found.
redis_cache/cache.py
View file @
7d1f8558
...
@@ -202,11 +202,14 @@ class CacheClass(BaseCache):
...
@@ -202,11 +202,14 @@ class CacheClass(BaseCache):
if
timeout
is
None
:
if
timeout
is
None
:
timeout
=
self
.
default_timeout
timeout
=
self
.
default_timeout
try
:
try
:
value
=
int
(
value
)
value
=
float
(
value
)
# If you lose precision from the typecast to str, then pickle value
if
int
(
value
)
!=
value
:
raise
TypeError
except
(
ValueError
,
TypeError
):
except
(
ValueError
,
TypeError
):
result
=
self
.
_set
(
key
,
pickle
.
dumps
(
value
),
int
(
timeout
),
client
)
result
=
self
.
_set
(
key
,
pickle
.
dumps
(
value
),
int
(
timeout
),
client
)
else
:
else
:
result
=
self
.
_set
(
key
,
value
,
int
(
timeout
),
client
)
result
=
self
.
_set
(
key
,
int
(
value
)
,
int
(
timeout
),
client
)
# result is a boolean
# result is a boolean
return
result
return
result
...
...
tests/testapp/tests.py
View file @
7d1f8558
...
@@ -336,6 +336,15 @@ class RedisCacheTests(TestCase):
...
@@ -336,6 +336,15 @@ class RedisCacheTests(TestCase):
self
.
assertEqual
(
result
,
False
)
self
.
assertEqual
(
result
,
False
)
self
.
assertEqual
(
cache
.
get
(
"addkey1"
),
"value"
)
self
.
assertEqual
(
cache
.
get
(
"addkey1"
),
"value"
)
def
test_float_caching
(
self
):
self
.
cache
.
set
(
'a'
,
1.1
)
a
=
self
.
cache
.
get
(
'a'
)
self
.
assertEqual
(
a
,
1.1
)
def
test_string_float_caching
(
self
):
self
.
cache
.
set
(
'a'
,
'1.1'
)
a
=
self
.
cache
.
get
(
'a'
)
self
.
assertEqual
(
a
,
1.1
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment