Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
ic_server_welfare
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7f0c9062
authored
Aug 23, 2019
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
redis错误可以被全局函数捕获,这边不需要手动去捕获
parent
d2f95f4a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
113 deletions
app/Models/Integral.php
app/Models/RedisModel.php
app/Models/Integral.php
View file @
7f0c9062
...
...
@@ -7,7 +7,7 @@ namespace App\Models;
use
App\Http\Filters\QueryFilter
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Support\Facades\DB
;
use
phpDocumentor\Reflection\DocBlock\Tags\Reference\Fqsen
;
use
Common\Model\RedisModel
;
//活动红包模型
class
Integral
extends
Model
...
...
@@ -53,7 +53,7 @@ class Integral extends Model
}
else
{
$count
=
$redis
->
hlen
(
'ic_welfare_integrals'
);
}
$integrals
=
arraySequence
(
$integrals
,
'id'
,
'SORT_ASC'
);
$integrals
=
arraySequence
(
$integrals
,
'id'
,
'SORT_ASC'
);
return
[
'data'
=>
array_values
(
$integrals
),
'count'
=>
$count
];
}
...
...
@@ -84,12 +84,10 @@ class Integral extends Model
//这里如果没有数据放到缓存了,直接删除key
if
(
$data
)
{
$re
sult
=
$re
dis
->
hmset
(
'ic_welfare_integrals'
,
$data
);
$redis
->
hmset
(
'ic_welfare_integrals'
,
$data
);
}
else
{
$re
sult
=
$re
dis
->
del
(
'ic_welfare_integrals'
);
$redis
->
del
(
'ic_welfare_integrals'
);
}
return
$result
;
}
//添加单个红包
...
...
@@ -102,10 +100,7 @@ class Integral extends Model
if
(
!
$id
)
{
return
false
;
}
$result
=
$this
->
addIntegralToRedis
(
$id
,
$data
);
if
(
$result
===
false
)
{
throw
new
\Exception
(
'新增红包活动缓存数据失败'
,
ErrorCode
(
19
,
5
));
}
$this
->
addIntegralToRedis
(
$id
,
$data
);
return
true
;
},
5
);
...
...
@@ -117,9 +112,7 @@ class Integral extends Model
public
function
addIntegralToRedis
(
$id
,
$data
)
{
$redis
=
new
RedisModel
();
$result
=
$redis
->
hset
(
'ic_welfare_integrals'
,
$id
,
json_encode
(
$data
));
return
$result
;
$redis
->
hset
(
'ic_welfare_integrals'
,
$id
,
json_encode
(
$data
));
}
//更新单个红包活动
...
...
@@ -133,13 +126,10 @@ class Integral extends Model
}
if
(
$result
)
{
$result
=
$this
->
changeIntegralsFromRedis
([
$id
]);
if
(
$result
===
false
)
{
throw
new
\Exception
(
'更新红包活动缓存数据失败'
,
ErrorCode
(
20
,
5
));
}
$this
->
changeIntegralsFromRedis
([
$id
]);
}
return
$result
?
true
:
fals
e
;
return
tru
e
;
},
5
);
return
$result
;
...
...
@@ -150,14 +140,11 @@ class Integral extends Model
{
$redis
=
new
RedisModel
();
$integral
=
$redis
->
hget
(
'ic_welfare_integrals'
,
$id
);
$result
=
false
;
if
(
$integral
)
{
//为什么用array_merge,因为右边的数组的键值会覆盖左边的对应值
$data
=
array_merge
(
json_decode
(
$integral
,
true
),
$data
);
$re
sult
=
$re
dis
->
hset
(
'ic_welfare_integrals'
,
$id
,
json_encode
(
$data
));
$redis
->
hset
(
'ic_welfare_integrals'
,
$id
,
json_encode
(
$data
));
}
return
$result
;
}
...
...
@@ -169,10 +156,7 @@ class Integral extends Model
if
(
!
$result
)
{
return
false
;
}
$result
=
$this
->
changeIntegralsFromRedis
(
$ids
);
if
(
$result
===
false
)
{
throw
new
\Exception
(
'更新部分红包信息到redis失败'
,
ErrorCode
(
21
,
5
));
}
$this
->
changeIntegralsFromRedis
(
$ids
);
return
true
;
},
5
);
...
...
@@ -199,16 +183,10 @@ class Integral extends Model
$redis
=
new
RedisModel
();
if
(
$needDelete
)
{
$result
=
$redis
->
hdel
(
'ic_welfare_integrals'
,
$needDelete
);
if
(
$result
===
false
)
{
return
false
;
}
$redis
->
hdel
(
'ic_welfare_integrals'
,
$needDelete
);
}
if
(
$needUpdate
)
{
$result
=
$redis
->
hmset
(
'ic_welfare_integrals'
,
$needUpdate
);
if
(
$result
===
false
)
{
return
false
;
}
$redis
->
hmset
(
'ic_welfare_integrals'
,
$needUpdate
);
}
return
true
;
...
...
app/Models/RedisModel.php
deleted
100644 → 0
View file @
d2f95f4a
<?php
namespace
App\Models
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Support\Facades\Redis
;
class
RedisModel
extends
Model
{
const
WRITE_CONNECT_METHOD
=
[
'set'
,
'del'
,
'rpush'
,
'lpush'
,
'expire'
,
'hset'
,
'hmset'
,
'hdel'
,
'hsetnx'
,
'hincrby'
];
private
$read
=
[];
private
$write
=
[];
public
function
__construct
(
$ConnectWrite
=
'default'
,
$ConnectRead
=
'read'
)
{
parent
::
__construct
();
$this
->
read
=
Redis
::
connection
(
$ConnectRead
);
$this
->
write
=
Redis
::
connection
(
$ConnectWrite
);
}
public
function
__call
(
$method
,
$args
)
{
$cls
=
&
$this
;
$tmp
=
function
(
$method
,
$args
)
use
(
$cls
)
{
if
(
strpos
(
$method
,
'pipeline_'
))
{
//使用管道
$method
=
substr
(
$method
,
9
);
if
(
in_array
(
$method
,
$cls
::
WRITE_CONNECT_METHOD
))
{
return
$cls
->
write
->
pipeline
(
function
(
$pipe
)
use
(
$args
)
{
foreach
(
$args
as
$arg
)
{
$pipe
->
$method
(
...
$arg
);
}
});
}
else
{
try
{
return
$cls
->
read
->
pipeline
(
function
(
$pipe
)
use
(
$args
)
{
foreach
(
$args
as
$arg
)
{
$pipe
->
$method
(
...
$arg
);
}
});
}
catch
(
ConnectionException
$e
)
{
return
$cls
->
write
->
pipeline
(
function
(
$pipe
)
use
(
$args
)
{
foreach
(
$args
as
$arg
)
{
$pipe
->
$method
(
...
$arg
);
}
});
}
}
}
else
{
if
(
in_array
(
$method
,
$cls
::
WRITE_CONNECT_METHOD
))
{
return
$cls
->
write
->
$method
(
...
$args
);
}
else
{
try
{
return
$cls
->
read
->
$method
(
...
$args
);
}
catch
(
ConnectionException
$e
)
{
return
$cls
->
write
->
$method
(
...
$args
);
}
}
}
};
try
{
return
$tmp
(
$method
,
$args
);
}
catch
(
\Exception
$e
)
{
dd
(
$e
);
return
false
;
}
// if (strpos($method, 'catch_') === 0) {
// $method = substr($method, 6);
// try {
// return $tmp($method, $args);
// } catch (\Exception $e) {
// return null;
// }
// } else {
// return $tmp($method, $args);
// }
}
}
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